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 pattern matching on type in for comprehensions #900
Comments
Imported From: https://issues.scala-lang.org/browse/SI-900?orig=1 |
@dcsobral said: Pattern match works on val assignments, for-comprehensions and case statements. Only there's an exception on for-comprehensions. Worse still, not an exception of an edge case no one is likely to use, or which would be hard to read, but an exception preventing an useful behavior. Right now, one can't express something like "for all ints in this list" in a direct way. |
@retronym said: scala> val xs = Seq(Some(1), None)
xs: Seq[Option[Int]] = List(Some(1), None)
scala> for { x @ (_x : Some[_]) <- xs } yield x
res0: Seq[Some[Any]] = List(Some(1)) Unfortunately |
@milessabin said: scala> val l = List(1, "foo", null)
l: List[Any] = List(1, foo, null)
scala> for (s @ (_x : String) <- l) yield s
res0: List[String] = List(foo) |
Erik Bruchez (ebruchez) said: The current behavior is really confusing and it would be great if it could be fixed. |
@SethTisue said: I'm not saying I know what the right thing to do is, because I don't. does anyone understand why, in Jason's workaround, _ can't be used in place of x or _x? if that weren't true, Jason's thing would be, like, allllllllmost something I could put up with. but if I am forced to supply a variable name I don't use, well, for me that pushes it over the line from "ok maybe" to "sad". |
@retronym said: scala> object Typed { def unapply[A](a: A) = Some(a) }
defined module Typed
scala> for { Typed(a: Int) <- List(1, "2") } yield a
res1: List[Int] = List(1) |
Piotr Czapla (pczapla) said: Anyway +1 to fix this. |
Ben Challenor (bchallenor) said: |
Max Bolingbroke (batterseapower) said: |
@densh said (edited on Jan 30, 2014 10:35:04 AM UTC): |
@densh said: |
@milessabin said: |
An improvement that allows underscores in the workaround, the gist of which is to change the pattern from simple name or typed name such as in an ordinary val def. The workaround also gibes with
|
in Dotty you just throw in scala> for (case x : Some[_] <- List(None, Some(1))) yield x
val res1: List[Some[_]] = List(Some(1)) a simpler example: scala> for (x: String <- List(1, "foo")) yield x
1 |for (x: String <- List(1, "foo")) yield x
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| Found: String => String
| Required: Any => String
scala> for (case x: String <- List(1, "foo")) yield x
val res2: List[String] = List(foo) |
It would be nice if pattern matches on type worked inside for comprehensions:
I'd like the middle one to be a pattern match on type, so instead of a type error it would give List(Some(1)).
The text was updated successfully, but these errors were encountered: