Open scabug opened 14 years ago
Imported From: https://issues.scala-lang.org/browse/SI-2823?orig=1 Reporter: @paulp See #9158, #1492 Duplicates #1305
@odersky said: The problem I see is the interaction with many other things. The `a' might be a pattern -- should the implicit then apply to all its variables? Then, the translation of generators in for expressions is quite involved. We'd have to speicfy how implicits are taken into account. This exceeds the design work usually associated with a ticket fix. I close the ticket but would welcome a SID that contains spec language to regulate all these things.
@odersky said: On second thought let's keep this open and assign to scala_community. Maybe someone picks up the ball and provides the spec enhancements.
Kim Stebel (kimstebel) said: Are there any guidelines on how to write such a spec?
About patterns: I think the behaviour should be analogous to
implicit val
where the implicit modifier does apply to all variables in the pattern.
@gkossakowski said: Unassigning and rescheduling to M7 as previous deadline was missed.
Mike Slinn (mslinn) said: +1. It would be great to be able to write:
for { implicit x <- Some(1) } yield x
Harold Lee (harold) said (edited on Apr 11, 2015 6:40:05 AM UTC): I've submitted a SIP for consideration for this and I'm working on an implementation.
SIP: https://github.com/scala/scala.github.com/pull/417
Code in progress: https://github.com/haroldl/scala/tree/sip24
@som-snytt said: I have a more modest PR in progress which just emits the implicit vals.
I'll also try to address #9158 by tupling extraction results instead of reapplying patterns downstream of midstream assignments. I see the SIP proposal pushes them into the body, which came up when Kim Stebel asked about it.
@retronym said: Link to [~apm]'s patch: https://github.com/scala/scala/pull/4730/files
Juan Manuel Serrano (jserrano) said:
Maybe this is the right place to raise another possible extension to the implicit
modifier (which I couldn't find elsewhere - sorry if it is). Namely, given, e.g.
trait MyStateMonad[F[_]] extends MonadState[F, Int]{
import scalaz.syntax.monad._
implicit val _ = this // necessary in order to use the syntactic sugar
...
}
it'd be nice to be able to write that just as follows:
trait MyStateMonad[F[_]] extends MonadState[F, Int]{ implicit this =>
import scalaz.syntax.monad._
...
}
Since https://contributors.scala-lang.org/t/implicit-keyword-in-lhs-of-for-comprehension/725/3 asked about the status:
There's also https://github.com/scala/slip/issues/6 and https://github.com/scala/scala/pull/4730. The latter PR is probably the most complete (and least ambitious), but it was held back because a SIP was required (and the SIP wasn't championed).
Quoting: https://github.com/scala/slip/issues/6#issuecomment-158483965
pretty huge caveat: see meeting notes (scala/scala-lang#358). @odersky seemed uninterested in having this go forward unless it's for patterns in general, not just for for comprehensions in particular. that's substantially more ambitious.
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")
@SethTisue wrote in https://github.com/scala/slip/issues/6#issuecomment-263976481 on Nov 30, 2016:
Closing since this repository is being decommissioned (#35).
This could still go forward under the current SIP process if someone steps forward to champion it.
Just to let anyone arriving here know, it's already possible to do that with better-monadic-for
.
More info: https://github.com/oleg-py/better-monadic-for#define-implicits-in-for-comprehensions-or-matches
😉
dotty syntax reminder
scala> for given Int <- List(42) yield summon[Int]
val res0: List[Int] = List(42)
scala> for case (i @ given Int) <- List(42) yield summon[Int] + i
val res1: List[Int] = List(84)
scala 2 syntax reminder
Welcome to Scala 2.13.12 (OpenJDK 64-Bit Server VM, Java 21).
Type in expressions for evaluation. Or try :help.
scala> object X { def unapply(s: String): Option[(String,Int)] = { val x = s.split("/").head ; Option(x, x.length) } }
object X
scala> implicit val X(s, i) = "ab/bbc"
^
warning: Implicit definition should have explicit type (inferred String) [quickfixable]
^
warning: Implicit definition should have explicit type (inferred Int) [quickfixable]
val s: String = ab
val i: Int = 2
scala> implicit val X(s: String, i: Int) = "ab/bbc"
val s: String = ab
val i: Int = 2
scala> implicitly[String]
val res0: String = ab
It looks like the last piece was just "pattern bound implicit" syntax
scala> Some((42, "hi")) match { case implicit Some((i: Int, s: String)) => implicitly[String] }
https://github.com/scala/slip/issues/6#issuecomment-158507383
Outgrowth of #1492, but that ticket has already been closed twice and the most important case works great. But as mentioned in its comments it would be useful and more consistent if this also worked: