Jonathan Ellis
06/03/2022, 5:28 PMFrancesc
06/03/2022, 5:38 PMforEach uses an iterator, so if you care about that (you need your loop to be as performant as possible), then you should use the for loop, otherwise it's a matter of preference.ephemient
06/03/2022, 5:45 PMforEach when it is meaningfully more useful, e.g.
foo["bar"]?.forEach { ... }
is more work to express with for. but otherwise use forephemient
06/03/2022, 5:48 PMit should only use an iterator when
@Ruckus not true; it always uses an iterator because that's resolved before inlining happens. e.g.
for (i in 1..10) {} // compiler optimizes this to an increment
(1..10).forEach {} // constructs range object, iterator, and boxesJonathan Ellis
06/03/2022, 6:03 PMmkrussel
06/03/2022, 6:11 PMPrefer using higher-order functions (https://kotlinlang.org/docs/coding-conventions.html#loops,filteretc.) to loops. Exception: `forEach`(prefer using a regularmaploop instead, unless the receiver offoris nullable orforEachis used as part of a longer call chain).forEach