In scala source, I found:
case object Nil extends List[Nothing] {
...
}
I can't understand why it is declared as case object
rather than object
?
I found this question [ Difference between case object and object ] is useful, and I guess this reason is the key:
default implementations of serialization
because we often send list of data to another actor, so Nil must be serializable, right?
With the provided answers(thanks), I try to write some code to verify it:
trait MyList[+T]
object MyNil extends MyList[Nothing]
val list: MyList[String] = MyNil
list match {
case MyNil => println("### is nil")
case _ => println("### other list")
}
You can see MyNil
is not case object
, but I can still use it in pattern matching. Here is the output:
### is nil
Do I misunderstand something?