23 ms·
Are you sure? Kotlin allows for one-line functions. This is valid Kotlin: fun sum(a:Int, b:Int) = a+b In fact, it is even shorter than the lambda equivale
by TrianguloY 3y ago
Are you sure? Kotlin allows for one-line functions. This is valid Kotlin:
fun sum(a:Int, b:Int) = a+b
In fact, it is even shorter than the lambda equivalent:
val sum = {a:Int, b:Int -> a+b }
Even so, if you still need a block body but dislike the mandatory return, you can use a run body (where the last value will automatically be returned).
fun sum(a:Int, b:Int) = run {
doThings()
a+b
}
And even in that case you can use let, apply, and all the other syntactic sugar to allow beautiful flows and one-liners
- brabel 3y ago> if you still need a block body but dislike the mandatory return You know, in Groovy, the return is always optional... Kotlin copied the lambda syntax from Groovy but didn't copy that for some reason (notice how this makes the language inconsistent, as "some" blocks require a mandatory return, others don't... Rust for example, like Groovy, does not require return in either situation). But I remember some of my colleagues were shocked by Groovy not requiring a return :D OMG that makes the code so hard to read!!!!! People just tend to not like anything that goes against what they do in their main language even when that doesn't really make any sense.