Reputation: 13686
I have a Scala list for which I derive the following pair of outputs (Scala 2.10):
println(myList.getClass)`
// class scala.collection.immutable.$colon$colon
println(myList)
// List(1, 2, 3,....)
Could you please explain:
What is the meaning of $colon$colon
Why doesn't the output derived from getClass indicate this is a List, not just some collection?
Upvotes: 0
Views: 71
Reputation: 22085
First, $colon$colon
is the encoding in a JVM-friendly (but not human-friendly) of the class ::
. Recall that an empty list is the singleton Nil
, and that a non-empty list is a ::
(read "cons") with a head (which is an element) and a tail (which is again a list).
A non-empty list is therefore always an instance of this class ::
. But the compiler renames it to $colon$colon
for the JVM to be happy.
I'm not sure I understand your second question. The output of println(myList)
simply redirects to the method toString()
of List
, which (through a few more indirections), prints the string "List"
followed by the elements in parentheses. That's all.
Upvotes: 2