Search code examples
reflectionkotlinjava-interop

Kotlin: double colon (reflection) operator over


So I was working with VertX Web, trying to make it work with Kotlin. There's a router and you have to say something like

val vertx = Vertx.vertx()
val server = vertx.createHttpServer()
val router = Router.router(vertx)
server.requestHandler(router::accept)

But it doesn't work. What am I doing wrong? When I use it on Kotlin defined classes, it behaves normally. Is it done on purpose?

Whatever, I had to do it manually like this

server.requestHandler{router.accept(it)}

Solution

  • It is a known bug.

    See this issue.

    A workaround is to use a Lambda instead. e.g.

    class Foo {
      fun doWork(work: () -> Unit) {
        work()
      }
    }
    
    class Bar (val text: String) {
      fun printText() {
        println("${text}")
      }
    }
    
    val foo: Foo = Foo()
    val bar: Bar = Bar("Hello Kotlin!")
    
    foo.doWork(bar::printText) //Fails
    foo.doWork({ bar.printText() }) //Is working