val x = for(i <- 1 to 3) yield i
x match {
case 1 :: rest => ... // compile error
}
constructor cannot be instantiated to expected type; found : collection.immutable.::[B] required: scala.collection.immutable.IndexedSeq[Int]
This is the same problem as MatchError when match receives an IndexedSeq but not a LinearSeq.
The question is, how to do it right? Adding .toList
everywhere doesn't seem right. And creating an own extractor which handles every Seq
(as described in the answer of the other question) would lead to a mess if everybody would do it...
I guess the question is, why can't I influence what the return type of sequence comprehensions is, or: why isn't such a generalized Seq
extractor part of the standard library?
val a +: b = 0 until 10
works likeval a :: b = (0 until 10).toList
does in 2.9. – SyckNil
as well? – CoworkerSeq(a, b, c)
syntax would be clearer in most situations where you want to match onNil
. – SyckSeq()
. Although according to this answer: https://mcmap.net/q/663020/-nil-and-list-as-case-expressions-in-scala,case List() =>
was a lot slower thancase Nil =>
before a dubious hack was put in to convertList()
toNil
. So I hope that some way is found to ensure good performance. – Coworker