https://kotlinlang.org logo
#getting-started
Title
# getting-started
j

John Guerra

09/18/2020, 9:32 PM
I believe it would be more appropriate to post this here, but is there any actual problem with utilizing Units? I've heard mixed stuff about them and how you should use open brackets rather than utilizing a Unit For example
Copy code
override fun onEnable() {
  plugin.server.onlinePlayers.forEach(::join)
}
Rather than
Copy code
override fun onEnable() = plugin.server.onlinePlayers.forEach(::join)
n

nanodeath

09/18/2020, 9:34 PM
I think it'd be clearer to say
onEnable(): Unit = ...
if you go with the latter
it is strictly preference...I don't think there's much difference either way. I do use expression syntax almost whenever I can though.
j

John Guerra

09/18/2020, 9:36 PM
Yeah, my apologies. I should of said it more correctly and labeled it as an "Expression body". I figured it was preference. Personally, I find using it nicer in some scenarios, though in others I find utilizing a block body nicer to look at. I thought there'd possibly be some form of benefit otherwise.
r

Ruckus

09/18/2020, 9:42 PM
Most of the arguments I've seen appear to boil down to how you interpret the meaning of
fun ... =
. From my experience, those who just view it as short hand / sugar tend to be okay with it, whereas those who view it as a form of assignment (semantically of course, not literally) tend to take issue. I'm not sure the reasoning or if I've just got some severe sample bias.
n

nanodeath

09/18/2020, 9:45 PM
when I was just learning Kotlin I got really confused between
val ... =
and
fun ... =
j

John Guerra

09/18/2020, 9:46 PM
Makes sense honestly. I can see why people would view it the way they do (Those who take issue of it). Personally, I never viewed it that way, but at the same time, everyone interprets things differently at times.
@nanodeath, same. Don't get me wrong, still learning. Still pretty fresh to the language, but I think I am getting the hand of it. I find developing in Kotlin much nicer.
j

Jakub Pi

09/19/2020, 3:06 AM
The only issue I've seen is that if you use the bracket syntax but don't make the return type explicit as with
: Unit
then in some rare cases, the type inference may fail. I've seen it happen once or twice in real code (though probably not with Unit as the return type - some heavy generics involved).