https://kotlinlang.org logo
Title
k

kingsley

06/03/2017, 7:43 AM
val handler: Handler = { req, res -> res.body("Hello") }
Is it bad practice? To declare lambdas? No, it's perfectly fine.
t

tipsy

06/03/2017, 7:44 AM
kingsley: that works with the java version, but if i convert the interface to kotlin:
@FunctionalInterface
@Throws(Exception::class)
fun handle(request: Request, response: Response)
i need to add a constructor in order to do that
i'm not sure how smart the conversion tool is
k

kingsley

06/03/2017, 7:47 AM
Yes. Kotlin interfaces are not SAM capable. You'd need use a functional type with typealias instead to achieve the same
typealias Handler = (Request, Response) -> Unit
t

tipsy

06/03/2017, 7:48 AM
thanks, that helps
k

kingsley

06/03/2017, 7:48 AM
You're welcome
t

tipsy

06/03/2017, 7:51 AM
maybe the best approach here would be to keep the functional interfaces as java, since i intend to use this with both java and kotlin? or is there a better approach?
k

kingsley

06/03/2017, 8:13 AM
Yes. You can do that. Using Kotlin's functional type results in
FunctionN
interfaces in Java which might not be as pretty as a specialized interface like your
Handler
Well. Except you intend for a 100% KT src, in which case, you'll eventually end up converting everything anway