Kotlin: double colon (reflection) operator over

2k views Asked by At

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)}
2

There are 2 answers

2
D3xter On BEST ANSWER

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
0
Jacob Zimmerman On

Technically it's not a bug. I asked early on if they planned to support method references on instances in version 1, and I was told that they most likely wouldn't.

Method references can only be used from classes and modules, not from instances. Coming from Java 8, this seems like a big deal, but considering the potential conciseness of their lambda syntax, it really isn't.

UPDATE: They plan to add this feature in 1.1