This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Pattern matching on a Seq as if it were a List -- can we get a warning?

1 reply
Alex Cruise
Joined: 2008-12-17,
User offline. Last seen 2 years 26 weeks ago.
Hey folks,

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
extempore
Joined: 2008-12-17,
User offline. Last seen 35 weeks 3 days ago.
Re: Pattern matching on a Seq as if it were a List -- can we ge

On Fri, Jan 6, 2012 at 11:25 AM, Alex Cruise <alex [at] cluonflux [dot] com> wrote:
How feasible would it be for nsc to issue a warning in this situation?

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.

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland