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

前端 未结 4 1948
后悔当初
后悔当初 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:52

    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
    

提交回复
热议问题