The following is a method rewritten in Kotlin from Java:
fun publishMessageSource(
name: String,
address: String,
completionHandler: Handler<AsyncResult<Unit>>
) {
val record = MessageSource.createRecord(name, address)
publish(record, completionHandler)
}
However, when I call it as follows:
publishMessageSource("market-data", ADDRESS, { record: Handler<AsyncResult<Unit>> ->
if (!record.succeeded()) {
record.cause().printStackTrace()
}
println("Market-Data service published : ${record.succeeded()}")
})
I get the error Type Mismatch required Handler<AsyncResult<Unit>> found (Handler<AsyncResult<Unit>>) -> Unit.
What am I doing wrong?
Your lambda should take the parameter that the single method of the
Handlerinterface takes, which isAsyncResult<Unit>in this case. Your lambda is theHandler, so it doesn't take theHandleras a parameter.I think you'll also need an explicit call to the SAM constructor here, since your function is written in Kotlin, that would look something like this:
This creates a
Handler<AsyncResult<Unit>>with a lambda representing its single method.Finally, you can omit the type inside the lambda to be less redundant: