1

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)}
Jayson Minard
  • 84,842
  • 38
  • 184
  • 227
caeus
  • 3,084
  • 1
  • 22
  • 36

2 Answers2

2

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
D3xter
  • 6,165
  • 1
  • 15
  • 13
  • It would be good to copy some content from the issue to here that explains what cases cause the bug. It can maybe be inferred from the question, but that isn't the same as "when you do X, it fails, but Y is ok" – Jayson Minard Jan 06 '16 at 03:14
  • I wouldn't call it a bug. It was simply never added as a feature. – Jacob Zimmerman Nov 30 '16 at 00:29
1

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

Jacob Zimmerman
  • 1,521
  • 11
  • 20