Skip to content

Guards in for loops do not compile #21

@rfmejia

Description

@rfmejia

for loops in Scala 3 (and 2.x) support guards, however the following does not compile:

def getNums(): UIO[List[Int]] = ZIO.succeed(List(1, 2, 3))
def printNum(n: Int): UIO[Unit] = ZIO.succeed(println(n))
defer {
  for {
    n <- getNums().run
    if n % 2 == 0
  } printNum(n).run // Error: While expanding a macro, a reference to parameter n
}                   // was used outside the scope where it was defined

It seems it has something to do with how guards are desugared, the following also doesn't compile:

defer { 
  getNums().run.withFilter(n => n % 2 == 0).foreach(n => printNum(n).run)
}

whereas replacing withFilter with filter successfully compiles.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions