I found a strange working construct in scala:
(ArrayBuffer[Int]():Seq[Int]) match {
case Nil => "whoo"
case _ => "nayyy"
}
which returns
"whoo"
Apparently this already works partially for Vectors, but not pattern matching. can someone explain me that? Nil does not have any method named unapply. How is this possible?
With objects,
unapplyis not involved (that would be the case if you had used the hypotheticalcase Nil() => ...). Instead, the equality is checked using theequalsmethod.Equality for collections is defined in terms of their elements. E.g.
The same happens with
Nilwhich equals any empty sequence: