Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pattern Matching "case Nil" for Vector

Tags:

scala

After reading this post on how to use pattern matching on Vector (or any collection that implements Seq), I tested pattern matching on this collection.

scala> x // Vector
res38: scala.collection.immutable.Vector[Int] = Vector(1, 2, 3)

scala> x match {
     |    case y +: ys => println("y: " + "ys: " + ys)
     |    case Nil => println("empty vector")
     | }
<console>:12: error: pattern type is incompatible with expected type;
 found   : scala.collection.immutable.Nil.type
 required: scala.collection.immutable.Vector[Int]
Note: if you intended to match against the class, try `case _: <none>`
                 case Nil => println("empty vector")
                      ^

Here's dhg's answer that explains +::

object +: {
  def unapply[T](s: Seq[T]) =
    s.headOption.map(head => (head, s.tail))
}

REPL shows me that

scala> Vector[Int]() == Nil
res37: Boolean = true

... so why can I not use this case Nil statement for an Vector?

like image 787
Kevin Meredith Avatar asked Nov 03 '13 19:11

Kevin Meredith


1 Answers

The comparison Vector[Int]() == Nil is possible because there is no constraint on the type level for what you compare; that allows the implementation of equals for collections, on the other hand, to perform an element by element comparison irrespective of the collection type:

Vector(1, 2, 3) == List(1, 2, 3)  // true!

In pattern matching, you cannot have a case for an empty list (Nil) when the type is not related to list (it's a Vector).

You can do this however:

val x = Vector(1, 2, 3)

x match {
  case y +: ys => println("head: " + y + "; tail: " + ys)
  case IndexedSeq() => println("empty vector")
}

But I would suggest just to use the default case here, because if x does not have a head element, it must be technically empty:

x match {
  case y +: ys => println("head: " + y + "; tail: " + ys)
  case _ => println("empty vector")
}
like image 173
0__ Avatar answered Oct 22 '22 23:10

0__