Why is for (x <- range) not optimised yet?

So following up on this, and double-checking with :javap, I wonder if there is a deeper reason why

for (x <- (range: Range)) ...

is not compiled to while loops yet by scalac (tested in 2.12)? Wouldn’t that be a rather trivial optimisation, and one that would apply in many many cases?


Because no one knows how to use the optimizer flags.

Even if it were optimized, it would sit behind a flag that’s never enabled. It wouldn’t be the deprecated flag you’ve been using, it would be that other flag you haven’t heard of if you weren’t reading the PR comments.


That’s -opt:l:magick, right?

Or, if EPFL are supportive, -opt-in-dotty.

spire has cfor which does exactly that.


I thought scala.js already does it.

1 Like

Scala.js does it, yes. It comes down to @som-snytt’s comment: in Scala.js the optimizer is enabled by default, but not in Scala/JVM. And that it can do because it can always work in a close world.

1 Like