Is there any special case class for representing an empty ArrayBuffer that can be used in pattern matching similar to Nil for lists?
Also why this works:
scala> collection.mutable.ArrayBuffer.empty == Nil
res11: Boolean = true
While this does not:
scala> collection.mutable.ArrayBuffer() match { case Nil => 1 }
<console>:8: error: pattern type is incompatible with expected type;
found : scala.collection.immutable.Nil.type
required: scala.collection.mutable.ArrayBuffer[Nothing]
UPDATE
After giving it some thought I presume there is no such a case class. While existence of Nil is vital for List to work, no special structure of this kind is needed for arrays.
I've found a workaround for empty match check that might work in most cases:
collection.mutable.ArrayBuffer(2) match {
case collection.mutable.ArrayBuffer(v, _*) => v * 2
case _ => 0
}
I first check if array has at least one element and otherwise it should be empty.
Also as it turns out I could just use ArrayBuffer.isEmpty instead of pattern match.
ArrayBuffer.isEmptyseems more right for a non-ADT! ...or justtoListyour ArrayBuffer, unless it's too expensive, and work on that.