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)}
It is a known bug.
See this issue .
A workaround is to use a Lambda instead. eg
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
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.