Get name of the enum in scala 3?

Viewed 271

Java enum has name() method but scala 3 doesn't have it.

How to get name of the enum in scala 3?

enum Color:
  case Red, Green, Blue
1 Answers

toString() method gives the name of the enum.

enum Color:
  case Red, Blue, Yellow

println(Color.Red.toString()) //=> Red

Scala 3 enum can also extend java Enum class and use the name() method.

enum Color extends Enum[Color]:
  case Red, Blue, Yellow

println(Color.Red.name()) //=> Red
Related