Scala enums are implemented as anonymous classes. Class[_].getSimpleName() always returns empty strings for them. Because of that in order to get to the information of the class name we need use toString on matched type. toString can potentially be overridden. In case of non-singleton enum we also need to parse the toString method to strip arguments list.
trait Named {
def name: String = {
this match {
// Singleton: case Foo
case e: scala.runtime.EnumValue =>e.toString()
// Non-singleton: case Foo(args)
case e: scala.reflect.Enum => e.toString.takeWhile(_ != '(')
// Non enum and not an anonymous class
case _ => this.getClass().getSimpleName().ensuring(_.nonEmpty)
}
}
}
enum Foo extends Named {
case Bar
case Other(foo: String)
}
case class NonEnum(arg: String) extends Named
Seq(Foo.Bar, Foo.Other("foo"), NonEnum("foo"))
.map(_.name)
.foreach(println)
output
// defined trait Named
// defined class Foo
// defined case class NonEnum
Bar
Other
NonEnum
The compiler has the information about the actual names for all enums, however, they're never used in case of enums non extending java.lang.Enum.
I propose to work on establishing the common pattern to accessing the names of the enums. One potential solution I can think of is usage of typeclass scala.runtime.EnumName[T <: scala.reflect.Enum] which instance would be created in the companion class of enum.
Scala enums are implemented as anonymous classes.
Class[_].getSimpleName()
always returns empty strings for them. Because of that in order to get to the information of the class name we need usetoString
on matched type.toString
can potentially be overridden. In case of non-singleton enum we also need to parse thetoString
method to strip arguments list.output
The compiler has the information about the actual names for all enums, however, they're never used in case of enums non extending java.lang.Enum. I propose to work on establishing the common pattern to accessing the names of the enums. One potential solution I can think of is usage of typeclass
scala.runtime.EnumName[T <: scala.reflect.Enum]
which instance would be created in the companion class of enum.