New feature preview: implicit definitions inside for-comprehensions
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, will work, but not , , or . 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.