Closed stacycurl closed 8 years ago
Doesn't unapplySeq
help here?
No (but I did panic for a bit, I've never actually used unapplySeq)
The following code prints false, false, true, indicating that unapplySeq can be used to match the entire contents of a list, but not a single element.
object Contains extends App {
def unapplySeq[T](x: List[T]): Option[List[T]] = Some(x)
println(List(1, 2, 3) match {
case Contains(2) => true
case _ => false
})
println(List(1, 2, 3) match {
case Contains(4) => true
case _ => false
})
println(List(1, 2, 3) match {
case Contains(1, 2, 3) => true
case _ => false
})
}
To resurrect this issue, please rework it as an issue/PR against Lightbend Scala (ie. scala/scala).
Ever 3 months I attempt to write an extractor like this:
At which point I remember that it's impossible but that's only the case because extractors have to return an Option (I'm ignoring the Boolean variant here), what if that were generalized.
Here's code for a normal extractor along with the decompiled java that's produced.
Here's code for a generalised extractor that returns a List instead of option along with imagined compiled code to support that.