Better Monadic For Versions Save

Desugaring scala `for` without implicit `withFilter`s

0.3.0-M1

5 years ago

Available on Maven Central:

addCompilerPlugin("com.olegpy" %% "better-monadic-for" % "0.3.0-M1")

A new feature comes to better-monadic-for, enabled by the flag -P:bm4:implicit-patterns (also enabled by default). This introduces a new keyword, implicit0, which can appear inside patterns, including those in for comprehensions:


case class ImplicitTest(id: String)

 for {
  x <- Option(42)
  implicit0(it) <- Option(ImplicitTest("eggs"))
  _ <- Option("dummy")
  _ = "dummy"
   _ = assert(implicitly[ImplicitTest] eq it)
} yield "ok"

This also works in regular match clauses:

(1, "foo", ImplicitTest("eggs")) match {
  case (_, "foo", implicit0(it)) => assert(implicitly[ImplicitTest] eq it)
}

The main goal is to support effectfully-created tagless algebras, but possibilities are large, so if you find a creative use for it, poke me on twitter (oleg.pyzhcov) or gitter to share!

Current limitations

As of this release, only simple named patterns can be used in implicit definition. That means, implicit0(x) will work, but not implicit0(x, y), implicit0(Some(x)), implicit0(_) or implicit0(x: ImplicitTest). The idea is to keep it as close as possible to a simple implicit val definition, so the latter restriction is the only one that's temporary.

This also obviously doesn't play nice with IDEs. I will be looking into making an Intellij plugin, but no promises. In the meantime, you can use a stub extractor:

object implicit0 {
  def unapply[A](a: A): Option[A] = Some(a)
}