Lilly
10/10/2021, 3:38 PMrepeat
. In the first place it's not clear what a return@repeat
does. I would expect it leaves the iteration but instead it skips the iteration. AFAIK it's not possible to escape from repeat
??!! And I guess the name does not implicitly tell that the repetition has to be finished. A repeatUntil
counterpart would help here. Another example where a return
is missleading: map
, mapValues
, mapKeys
, associateBy
, associateWith
etc.
Here I would expect that a return
skips the current item, but instead the return
gets part of data (Unit
). Some ..NotNull
counterparts would help here. What do you guys think about that?Adam Powell
10/10/2021, 4:41 PMwhile
and for
loops work great and support both continue
and break
🙂 saving 1-3 lines of code isn't a very big payoff for having to learn and remember dozens of edge case stdlib functions to read kotlinLilly
10/10/2021, 4:51 PMephemient
10/10/2021, 6:54 PMreturn
always returns from the nearest enclosing function, even if it's a non-local return (only possible inside inline lambdas), while return@label
returns from a labeled lambda, where the label can be explicit label@{ return@label }
or otherwise is implicitly labeled by the name of the function the lambda is being given toephemient
10/10/2021, 6:56 PMrepeat
or map
or anythingephemient
10/10/2021, 6:58 PMrun {
repeat(1) {
return@run
}
}
but why would you do that instead of a for
loop that you can simply break
from?Lilly
10/12/2021, 1:54 PMassociateWith
and want to skip nullables, you can't do this without writing your own extension function or general kotlin codeephemient
10/12/2021, 5:13 PMinline fun <K, V: Any> Iterable<K>.associateWithNotNull(
valueTransform: (K) -> V?
): Map<K, V> = mapNotNull {
it to (valueTransform(it) ?: return@mapNotNull null)
}.toMap()
or
buildMap {
for (key in this@associateWithNotNull) {
put(key, valueTransform(key) ?: continue)
}
}
you could propose it to be added to stdlib but presumably they didn't anticipate enough usage to make this shortcutLilly
10/13/2021, 12:21 PM