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

Ellen Spertus

10/25/2019, 11:38 PM
What can I pass to a Java method that expects a
Void!
?
The Java class is parameterized.
p

Pablichjenkov

10/25/2019, 11:40 PM
Did you try
null
is the only thing that comes to my mind.
e

Ellen Spertus

10/25/2019, 11:41 PM
@Pablichjenkov Thank you! That works. I didn't think of that because of the
!
.
p

pt

10/26/2019, 12:08 AM
If memory serves, there is no instance of a
Nothing
type, null is the only value of a
Nothing?
type
p

Pablichjenkov

10/26/2019, 12:12 AM
Exactly, I missed the nullable mark
s

streetsofboston

10/26/2019, 1:04 AM
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

karelpeeters

10/26/2019, 9:03 AM
Java's
Void
is more like Kotlin
Nothing?
than
Unit
.
s

streetsofboston

10/26/2019, 1:36 PM
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

karelpeeters

10/26/2019, 1:37 PM
Well primitive
void
is nothing like the class
Void
.
s

streetsofboston

10/26/2019, 1:47 PM
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

Pablichjenkov

10/26/2019, 2:34 PM
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

karelpeeters

10/26/2019, 2:59 PM
void
is not like
Nothing
at all, void simply means a function doesn't return anything but
Nothing
means it never returns!
👍 2
2 Views