In Scala, how do I get the *name* of an `object` (not an instance of a class)?
Just get the class object and then its name.
scala> Thingy.getClass.getName
res1: java.lang.String = Thingy$
All that's left is to remove the $
.
EDIT:
To remove names of enclosing objects and the tailing $
it is sufficient to do
res1.split("\\$").last
If you declare it as a case object
rather than just an object
then it'll automatically extend the Product
trait and you can call the productPrefix
method to get the object's name:
scala> case object Thingy
defined module Thingy
scala> Thingy.productPrefix
res4: java.lang.String = Thingy