allow forEach as an alternative to foreach#15368
allow forEach as an alternative to foreach#15368AugustNagro wants to merge 1 commit intoscala:mainfrom
Conversation
|
It would be something if lowercase identifiers were allowed to match ignoring case. Other names I would always write lowercase for this reason: The rule would apply for any member lookup, including for override checking. TIL the word "literal" can refer to a typo in a single letter, so I'd propose calling the language feature Under |
|
Supporting If we want to support for loops with Java collections, it should be restricted to for loops. |
|
why not just use an extension method on java collections providing foreach? or even better (if you ignore some minor sideeffects for the ecosystem): just rename foreach to forEach in general ;) |
That's a good idea, the problem I see is that it confuses users, who see both
That makes sense to me. Do you have an idea on how to implement? for-loop desugaring to |
odersky
left a comment
There was a problem hiding this comment.
I also think the right way to treat this is with an extension method.
Actually, it's the typer that calls An extension method on Java collections would work, of course. If it's not in Predef it would require an import, though. |
|
Thanks Will & Martin; I created #15379 which defines 2 extension methods in stdLibPatches/Predef.scala |
We have some problems with
foreachin Scala:1. Compatibility with Java libraries
This doesn't work, since Java uses
forEachon its collections. It's annoying that Java collections work infor-yieldloops, but notfor-do.2. Developer Expectations
Developers expect
forEach, notforeach, because the former is camel cased just likeflatMapand every other method. Java, JavaScript, C++, Kotlin, and the majority of popular languages caseforeachaccording to the language's regular style.I propose that in cases where
foreachis required but not resolved, we tryforEachas an alternative.This fixes the compatibility issue, while also allowing developers to use the case they prefer.
Questions:
for-doloop desugaring?