Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow implicit modifier in for comprehension #2823

Open
scabug opened this issue Dec 20, 2009 · 13 comments
Open

Allow implicit modifier in for comprehension #2823

scabug opened this issue Dec 20, 2009 · 13 comments
Labels
enhancement fixed in Scala 3 This issue does not exist in the Scala 3 compiler (https://github.com/lampepfl/dotty/) has PR help wanted
Milestone

Comments

@scabug
Copy link

scabug commented Dec 20, 2009

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:

for (implicit a <- ...) {
  ...
}
@scabug
Copy link
Author

scabug commented Dec 20, 2009

Imported From: https://issues.scala-lang.org/browse/SI-2823?orig=1
Reporter: @paulp
See #9158, #1492
Duplicates #1305

@scabug
Copy link
Author

scabug commented Dec 21, 2009

@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.

@scabug
Copy link
Author

scabug commented Dec 21, 2009

@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.

@scabug
Copy link
Author

scabug commented Jan 3, 2013

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.

@scabug
Copy link
Author

scabug commented Oct 15, 2013

@gkossakowski said:
Unassigning and rescheduling to M7 as previous deadline was missed.

@scabug
Copy link
Author

scabug commented Dec 5, 2014

Mike Slinn (mslinn) said:
+1. It would be great to be able to write:

for { implicit x <- Some(1) } yield x

@scabug
Copy link
Author

scabug commented Apr 11, 2015

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: scala/docs.scala-lang#417

Code in progress: https://github.com/haroldl/scala/tree/sip24

@scabug
Copy link
Author

scabug commented Sep 6, 2015

@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.

@scabug
Copy link
Author

scabug commented Nov 11, 2015

@retronym said:
Link to [~apm]'s patch: https://github.com/scala/scala/pull/4730/files

@scabug
Copy link
Author

scabug commented Mar 16, 2016

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._
      ...
}

@Blaisorblade
Copy link

Since https://contributors.scala-lang.org/t/implicit-keyword-in-lhs-of-for-comprehension/725/3 asked about the status:

There's also scala/slip#6 and scala/scala#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: scala/slip#6 (comment)

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")

Latest status

@SethTisue wrote in scala/slip#6 (comment) 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.

@adriaanm adriaanm removed their assignment Sep 28, 2018
@guizmaii
Copy link

guizmaii commented Jan 21, 2020

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

😉

@SethTisue SethTisue added the fixed in Scala 3 This issue does not exist in the Scala 3 compiler (https://github.com/lampepfl/dotty/) label Sep 13, 2020
@som-snytt
Copy link

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] }

scala/slip#6 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement fixed in Scala 3 This issue does not exist in the Scala 3 compiler (https://github.com/lampepfl/dotty/) has PR help wanted
Projects
None yet
Development

No branches or pull requests

6 participants