`return Unit.INSTANCE;`
# announcements
k
return Unit.INSTANCE;
n
kevinmost: Yes, I figured that, but what if the person calling this from java side is unaware of this class? I myself was under the impression of it behaving like java void.
k
It doesn't, because Kotlin doesn't have the concept of a function not having a return type. All functions return something, even if that something is
Unit
👍 1
the caller should see this
Unit
class and try to understand how they can construct one to return, and hopefully then they will see that all you can do with Unit is reference
Unit.INSTANCE
n
Thanks. I’m just getting started on this. This would be my first project on kotlin, so probably the doubts and questions might be silly.
👍 1
k
And why doesn't the compiler generate code that does actually return nothing, ie. generate void functions.
👍 1
k
Since the Kotlin functions get compiled as
FunctionN<Params..., ReturnType>
, you would need a generic, which
void
is not. Maybe
Void
could be used there but when you use
Void
you still have to
return null;
in Java, which I don't think is really any better than
return Unit.INSTANCE;
k
Well it could be compiled as
FunctionVoid
or something.
k
you'd end up with the same issues as other specialized types like
IntArray
though, where they wouldn't work generically alongside other `Function`s with the same set of arguments. I don't think the loss of that flexibility is worth not having to explicitly return
k
I see.
n
@kevinmost Hmm.. seems it will create specialized versions like they did with jdk..