Is it possible to create Nil
for maps?
I mean somethin similar to that:
List() match {
case Nil => true
}
but with map:
Map() match {
case NilMap => true
}
I tried to implement it, but I'm stuck:
object NilMap extends Map[Nothing, Nothin] {
def unapply[K,V](map: Map[K,V]): Option[Map[K,V]] =
if(map.isEmpty) Some(map) else None
}
but id doesn't compile...
This is called a Boolean extractor.
object NilMap {
def unapply(map: Map[_, _]): Boolean =
map.isEmpty
}
Map.empty[String, Int] match { case NilMap() => true; case _ => false } // true
Map("a" -> 10) match { case NilMap() => true; case _ => false } // false
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With