In the scala source, I found:
case object Nil extends List[Nothing] { ... }
I canβt understand why it is declared as a case object and not an object ?
I found this question [ The difference between an object of an object and an object ] useful, and I believe that this reason is the key:
default serialization implementation
because we often send a list of data to another player, so Nil has to be serializable, right?
With the answers provided (thanks), I'm trying to write 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 that MyNil not a case object , but I can still use it according to the pattern. Here is the result:
#
Am I misunderstanding something?
list null scala case
Freewind
source share