https://kotlinlang.org logo
#announcements
Title
# announcements
i

ilyagulya

07/14/2017, 4:29 PM
Hello, guys. How can i do such thing: I have project with Java and Kotlin interop. I want to use closures like this:
(Int) -> Unit
and provide them from Java to Kotlin. However, in Java i need to provide
Function1<Int, Unit>
instead of
Consumer<Int>
or something like this. Due to this limitation i need to return
Unit.INSTANCE
or use custom interfaces instead of closures How can i avoid this problem?
r

rednifre

07/14/2017, 4:36 PM
Couldn't you do fun bla_java(c: Consumer<Int>) and have that call the regular function that takes a Function1<Int, Unit> ?
i

ilyagulya

07/14/2017, 4:42 PM
Yes, i could do this. But isn't it better to use
(Int) -> Unit
but
Consumer<Int>
?
r

rednifre

07/14/2017, 4:46 PM
Yes, I meant you have bla(b: (Int) -> Unit) and bla(b: Consumer<Int>) = bla(x -> b.consume(x)), i.e. the java-friendly one that accepts a consumer just calls the "real" one that takes (Int) -> Unit.
i

ilyagulya

07/14/2017, 5:19 PM
oh, i see
However, it is not very clear approach, IMO
2 Views