Skip to content
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

Guards in for loops do not compile #21

Open
rfmejia opened this issue Dec 19, 2022 · 0 comments
Open

Guards in for loops do not compile #21

rfmejia opened this issue Dec 19, 2022 · 0 comments

Comments

@rfmejia
Copy link

rfmejia commented Dec 19, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant