https://kotlinlang.org logo
Title
d

Derek Peirce

05/22/2020, 7:15 PM
IntRange.forEach
uses
Iterable.forEach
, so
(1..n).forEach { ... }
creates an
Iterator
, while
for (k in (1..n)) { ... }
is optimized to use a while-loop, not even creating the original
IntRange
object. Could
IntRange
receive its own
forEach
extension method to preserve this optimization, and perhaps receive additional optimizations for common methods like
map
?
r

Ruckus

05/22/2020, 7:40 PM
Those are a little different. The for loop optimization is a compiler intrinsic, not a library function. Not that it invalidates your request, just that I don't think the two are very related.
i

ilya.gorbunov

05/22/2020, 8:36 PM
d

Derek Peirce

05/22/2020, 10:12 PM
@Ruckus it's a compiler intrinsic that a specialized inline function would be able to take advantage of, unlike an
Iterable
extension function.