object Initialization {
def main(args: Array[String]): Unit = {
val list = Traversable(1,2,4,5)
//list.foreach(println)
println( list.getClass.getSimpleName )
}
}
It prints $colon$colon, but I expected something like List. What does this obscure name mean?
That’s because
Listis a sealed superclass that is over case class::and objectNil(see the known subclasses section under the List documentation).::is pronounced cons, being named after the list cons operator in most traditional functional programming languages.Since your list head is actually an instance of the
::case class, and Scala uses name mangling to represent non-word class names on the JVM, you end up with$colon$colonwhen you use reflection to get the name.Update: I’m not exactly sure what you’re trying to use the class name for, but you might be able to use
stringPrefixinstead. It’s defined for anything extendingGenTraversableLike, which will be pretty much every Scala collection type.If you’re trying to do control flow with the class name, you should probably try something else. Pattern matching would work nicely, e.g.: