Skip to content

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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions compiler/src/dotty/tools/dotc/core/StdNames.scala
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,7 @@ object StdNames {
val flatMap: N = "flatMap"
val floatHash: N = "floatHash"
val foreach: N = "foreach"
val forEach: N = "forEach"
val format: N = "format"
val fromDigits: N = "fromDigits"
val fromProduct: N = "fromProduct"
Expand Down
2 changes: 2 additions & 0 deletions compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,8 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
assignType(tree2, TryDynamicCallType)
else
typedDynamicSelect(tree2, Nil, pt)
else if tree0.name == nme.foreach then
typedSelect(cpy.Select(tree0)(tree0.qualifier, nme.forEach), pt, qual)
else
assignType(tree,
rawType match
Expand Down
18 changes: 18 additions & 0 deletions tests/pos/forEach-desugar.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import java.util.{List, ArrayList}

object ForEachDesugar {

val listA = scala.List(1, 2, 3)
val listB = List.of(1, 2, 3)
val listC = ArrayList[Int]()

for i <- listB do listB.add(i)

for
x <- listA
y <- listB
z <- listC
do
listB.add(x + y)

}