isn’t that like `void`
# announcements
b
isn’t that like
void
l
It is like void, but returning Unit might seem like returning Java's
Void
(although you can't get Void instances, you can do so for Unit)
s
Unit
isn’t exactly like `void`—a void method doesn’t return anything at all, where a method that has a return type of
Unit
does return
Unit
, even if implicitly
in some ways
void
is more akin to Kotlin’s
Nothing
but yeah, you can return
Unit
. It’s a real
object
, you can reference it as a value, even write extension methods for it if you wanted
b
👍
s
Unit
is basically the object with only one value... It's a "unit", it can't change or hold multiple values. And yes, it is the
void
counterpart for Java. If you don't need or care about what a function returns, preferably use
Unit
as the return type.
n
It's not like void because you cannot create an instance of void at all
whereas
Unit
is a monostate type
which is a lot better
1
There are actually proposals in C++ to make void like this. It gets really annoying in generic code, if you want to call a function and put the return in a variable, and then do something, and return it later
this works for every single type, except void