https://kotlinlang.org logo
a

addamsson

10/04/2020, 2:49 PM
Hi there! I've seen that with 1.4 SAM intefaces are now converted properly between Kotlin and Java. This is great, but there is a problem, however: I still can't define functions that return void. For example if I have a signature like this:
Copy code
fun someFun(fn: (SomeClass) -> Unit)
I have to explicitly return
Unit.INSTANCE
from Java code. Is there a way to fix this (besides writing a helper function)
🚫 2
b

Big Chungus

10/04/2020, 3:31 PM
Try returning Nothing
e

ephemient

10/04/2020, 8:08 PM
Kotlin has a single
interface Function<out R>
type so as far as Java is concerned, you always need to return some value for
R
. for the sake of interop, you could define your own SAM or use Java's specialized SAMs returning void, e.g.
Copy code
fun someFun(fn: Consumer<SomeClass>)
a

addamsson

10/12/2020, 8:14 PM
Thanks.
2 Views