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


#1

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?


Pre-SIP: Desugaring If statements
#2

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.


#3

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

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


#4

spire has cfor which does exactly that.


Pre-SIP: Desugaring If statements
#5

I thought scala.js already does it.


#6

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.