What can I pass to a Java method that expects a `V...
# announcements
e
What can I pass to a Java method that expects a
Void!
?
The Java class is parameterized.
p
Did you try
null
is the only thing that comes to my mind.
e
@Pablichjenkov Thank you! That works. I didn't think of that because of the
!
.
p
If memory serves, there is no instance of a
Nothing
type, null is the only value of a
Nothing?
type
p
Exactly, I missed the nullable mark
s
A void/Void in Java is
Unit
in Kotlin and has only one value
Unit
. Passing a null (
Unit?
) may indeed work as well. The type
Nothing
has no values at all (which means you can't create one, get one or pass one). The type
Nothing?
has only one value (null) and is a sub-type of all other types
T?
(including
Unit?
).
👆 2
k
Java's
Void
is more like Kotlin
Nothing?
than
Unit
.
s
All Java methods returning the primitive
void
return
Unit
in Kotlin.
Void
is a nullable version of
void
in Java (much like
int
vs
Integer
), which would be
Unit?
in Kotlin.
k
Well primitive
void
is nothing like the class
Void
.
s
You're right about how
Void
is designed, with a private constructor and no public singleton or factory methods returning one, much like the design of
Nothing
.
p
In plain English, I would relate
Void
with Emptyness or
Nothing
. Don't know if the language designers pursued this semantic. Now as Anton said the fact of java methods returning
void
needing to return
Unit
in kotlin makes one to think that they are equivalent. To me this
void -> Unit
transformation is basically a trick to bridge an imperative paradigm with a functional paradigm. But when it comes to the typing system itself I am more into
Void
being more
Nothing
than
Unit
.
k
void
is not like
Nothing
at all, void simply means a function doesn't return anything but
Nothing
means it never returns!
👍 2