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

kevinmost

07/05/2017, 6:47 PM
return Unit.INSTANCE;
n

nayanjyoti

07/05/2017, 6:49 PM
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

kevinmost

07/05/2017, 6:50 PM
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

nayanjyoti

07/05/2017, 6:51 PM
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

karelpeeters

07/05/2017, 6:56 PM
And why doesn't the compiler generate code that does actually return nothing, ie. generate void functions.
👍 1
k

kevinmost

07/05/2017, 6:59 PM
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

karelpeeters

07/05/2017, 7:03 PM
Well it could be compiled as
FunctionVoid
or something.
k

kevinmost

07/05/2017, 7:21 PM
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

karelpeeters

07/05/2017, 7:23 PM
I see.
n

nayanjyoti

07/05/2017, 7:35 PM
@kevinmost Hmm.. seems it will create specialized versions like they did with jdk..
11 Views