2 ms·
Regarding the for loop, it's not magic either. "For comprehensions" in Scala are a wonderful syntax sugar that can look like familiar "for loops" for Java folk
by kenbot 15y ago
Regarding the for loop, it's not magic either. "For comprehensions" in Scala are a wonderful syntax sugar that can look like familiar "for loops" for Java folk, or familiar "do loops" for Haskell folk depending on how you use them.
But they always desugar to flatMap/map/withFilter method calls that you could have written yourself -- there is nothing "special" about them in that regard.
- barkmadley 15y agoThe desugaring process is what makes the for loop magic. I cannot write a different control structure that looks like the that for loop in scala without touching the compiler. A counter example that shows how to avoid the sugar would be "if expressions" in smalltalk (simplified, may differ to reality). In smalltalk, the abstract boolean class has a method 'if'. And the "true" subclass of the boolean class implements the 'if' method to execute its first parameter, while the "false" subclass implements the 'if' method to execute its second parameter.