I'm trying to call <https://docs.oracle.com/javase...
# announcements
a
I'm trying to call https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/CompletableFuture.html#exceptionally-java.util.function.Function- from Kotlin using a lambda:
Copy code
exceptionally { 
    it.printStackTrace()
}
But it complains
Type mismatch. Required: Void! Found: Unit
. Shouldn't have Kotlin handled this properly or am I missing something?
When I add
return null
, which should comply to the error as it's
Void!
then it complains that null is not a non-null type of Unit as it expects in Kotlin. So what is it Kotlin, a
Void!
or a
Unit
?
s
Are you explicitly writing
return null
within the lambda?
’cause that’ll return from the outer function
☝️ 2
in a scratch file, this seems to work:
Copy code
CompletableFuture<Void>().exceptionally {
  it.printStackTrace()
  null
}
a
🤦‍♂️
s
which is not great but gets your code compiling
a
You're right
😅 1
But why do I have to return null?
Inside of the lambda ofc
s
java.lang.Void
has been a compatibility sore spot for a while now, because there apparently isn’t an obvious way for
kotlinc
to cleanly map it to Unit, it seems, or at least, not in all instances
a
So mapping it here was missed?
s
I’m not entirely sure, this is a bit of interop knowledge that I don’t have unfortunately
I’m almost certain there’s a youtrack for it though
a
Alright, thank you!
g
Because replacing of Void with Unit works only for functions, but this particular lambda has generic Void on Java side (the same thing would happen for Kotlin lambda that returns Unit, you have to return null or Unit.INSTANCE on Java side), this is because of generics