In Scala, how do I get the *name* of an `object` (not an instance of a class)?

前端 未结 4 1908
后悔当初
后悔当初 2020-12-25 14:07

In Scala, I can declare an object like so:

class Thing

object Thingy extends Thing

How would I get \"Thingy\" (the name of th

4条回答
  •  無奈伤痛
    2020-12-25 14:59

    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
    

提交回复
热议问题