Simon Schäfer
unread,Oct 8, 2012, 9:20:15 AM10/8/12Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to scala-l...@googlegroups.com
I just noticed that pattern matching against types does not work in
for-comprehensions:
scala> for (s @ "a" <- List(1,"a","b")) println(s)
a
scala> for (s:String <- List(1,"a","b")) println(s)
<console>:11: error: type mismatch;
found : String => Unit
required: Any => ?
for (s:String <- List(1,"a","b")) println(s)
^
The compiler probably sees it as a normal type ascription, is this
expected behavior?
What's the best way to work around this? The unsugared expression looks
ugly and collect is not best use case because I'm not interested in the
return value.
scala> List(1,"a","b").filter(_.isInstanceOf[String]) foreach println
a
b
scala> List(1,"a","b") collect { case s: String => println(s) }
a
b
res23: List[Unit] = List((), ())