https://kotlinlang.org logo
Title
h

holgerbrandl

04/17/2018, 7:41 AM
Thanks @marstran, but if
Unit
is the
void
equivalent, why is it assignable in kotlin but not in java?
r

Ruckus

04/17/2018, 6:05 PM
This is a misunderstanding.
Unit
isn't the
void
equivalent. The problem is
void
in Java is playing two roles and doesn't distinguish between them. Java can't distinguish between a function that returns, but doesn't return a useful value (
Unit
in Kotlin
()
in Rust, etc.) and a function that doesn't return at all (usually called a diverging function) (
Nothing
in Kotlin,
!
in Rust, etc.)
Basically,
Unit
+
Nothing
is the equivalent of Java's overly vague
void
.