VirtusLab / avocADO

Safe compile-time parallelization of for-comprehensions for Scala 3
https://virtuslab.github.io/avocADO/docs/index.html
Apache License 2.0
87 stars 5 forks source link

Add an unneeded trailing map removing macro #115

Open KacperFKorban opened 2 months ago

KacperFKorban commented 2 months ago

We might want to provide a macro that removes the trailing map if it is unneeded. The use for it would be to make a for-comprehension style expression stack-safe, since flatMap can be (and often is) implemented in a stack-safe manner, but a map isn't. One such example is:

def loop: IO[Unit] =
  for
    _ <- putStrLn("fix this pls")
    _ <- loop
  yield ()

We might want to add a macro elimMap that would be used like so:

def loop: IO[Unit] =
  elimMap {
    for
      _ <- putStrLn("fixed")
      _ <- loop
    yield ()
  }

That will remove the trailing map call if it is unnecessary and give an error (or warning) if the map changes the result.