Hey guys! When talking about Kotlin libraries, the...
# announcements
l
Hey guys! When talking about Kotlin libraries, there's a specific part in the code style that states
Always explicitly specify function return types and property types (to avoid accidentally changing the return type when the implementation changes)
It's kind of ambiguous for me if it applies to
Unit
as well. As I understand, it's meant for
Copy code
fun test(someVar:String) = when(someVar){
 "a" -> 10
 else -> 20
}
where you can easily break the Int return. Would we always specify that a certain method returns Unit, even if it's redundant?
a
In my mind, a
fun
defined as a block (not with
=
) is explicitly returning
Unit
if its return type is not otherwise specified. At least, this is significantly different from the “inferred return type” of functions defined as expressions, and I never put the
: Unit
in. I do always define functions returning Unit as blocks, though.
👍 1