I want to get a simple class name regardless if the class is defined in the interpreter or in a scala code file. You can't get a simple name from a class defined in the interpreter (see here). As a result, I tried to create a method that would just get the fully qualified name, split it by .
, and grab the last string:
def simpleNameOf(i: AnyRef) = i.getClass.getName.split('.').last
However, here is when things got weird.
scala> def simpleNameOf(i: AnyRef) = i.getClass.getName.split('.').last
simpleNameOf: (i: AnyRef)Stringscala> simpleNameOf(new Human)
res25: String = $read$Humanscala> var h = new Human
h: Human = Human@fdad4abscala> h.getClass.getName
res27: String = Humanscala> h.getClass.getName == "Human"
res28: Boolean = falsescala> h.getClass.getName.length
res29: Int = 44scala> h.getClass.getName.split('.')
res30: Array[String] = Array($line102, $read$Human)scala> "Human".split('.')
res31: Array[String] = Array(Human)
So how in the heck is the string Human from getClass.getName
44 characters long? Where did $line102 & $read$Human come from?
This is Scala 2.10.4