- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Pattern matching on a Seq as if it were a List -- can we get a warning?
Fri, 2012-01-06, 20:25
Hey folks,
From http://youtrack.jetbrains.net/issue/SCL-3958:
How feasible would it be for nsc to issue a warning in this situation? From a scrutinee/pattern type agreement standpoint it's technically valid, but can never work for other types of Seq.
-0xe1a
From http://youtrack.jetbrains.net/issue/SCL-3958:
val someSeq: Seq[Int] = Vector(1,2,3)
// This throws a MatchError because someSeq isn't a list
someSeq match {
case x :: xs => "non-empty list starting with " + x
case Nil => "empty"
}
// This is a correct version that works with lists too
someSeq match {
case Seq(x, xs @ _*) => "non-empty seq starting with " + x
case Seq() => "empty"
}
How feasible would it be for nsc to issue a warning in this situation? From a scrutinee/pattern type agreement standpoint it's technically valid, but can never work for other types of Seq.
-0xe1a
On Fri, Jan 6, 2012 at 11:25 AM, Alex Cruise <alex@cluonflux.com> wrote:
Right now exhaustiveness checking is driven by the type of scrutinee. It appears you are suggesting we inspect the cases for matches which would be exhaustive if the scrutinee were a different type. I think this enters the neighborhood of "external lintlike tool".
> but can never work for other types of Seq.
All pattern matches could be described this way. Cases only match what they match and can never work for anything they don't match.