-
Notifications
You must be signed in to change notification settings - Fork 1.1k
allow forEach as an alternative to foreach #15368
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
foreach
in Scala:1. Compatibility with Java libraries
This doesn't work, since Java uses
forEach
on its collections. It's annoying that Java collections work infor-yield
loops, but notfor-do
.2. Developer Expectations
Developers expect
forEach
, notforeach
, because the former is camel cased just likeflatMap
and every other method. Java, JavaScript, C++, Kotlin, and the majority of popular languages caseforeach
according to the language's regular style.I propose that in cases where
foreach
is required but not resolved, we tryforEach
as an alternative.This fixes the compatibility issue, while also allowing developers to use the case they prefer.
Questions:
for-do
loop desugaring?