Is there a way to declare an implicit val inside a for comprehension?

Since version 0.3.0-M1, a better-monadic-for compiler plugin provides such functionality.


No, there isn't. There is a ticket though: https://issues.scala-lang.org/browse/SI-2823


Scala 3 (Dotty) enables givens (implicits) in for-comprehensions, for example

Starting dotty REPL...
scala> for {
     |   given _: Int <- Some(41)
     |   y <- Some(1)
     | } yield summon[Int] + y
val res0: Option[Int] = Some(42)

According to Implicits in for comprehensions/pattern matches SIP tracking #6

Martin points out that it Dotty already supports the more ambitious version as long as the types are annotated. so e.g. in Dotty this compiles: implicit val (a: Int, b: String) = (3, "foo")