ValueEnum
ValueEnum
is a new way for defining Scala enums, representing enum constants as final val
s in companion object of enum class.
Advantages:
- feature parity with Java enums (although
ValueEnum
does not extendjava.lang.Enum
) - every enum constant has
name
(name of thefinal val
that declares the constant) andordinal
(zero based index of enum constant, consistent with declaration order). - less boilerplate than sealed hierarchy based enums
- it's possible to declare many enum constants in a single line of code
- much less generated classes, which is advantageous primarily for JS size reduction
Disadvantages:
- scalac exhaustive pattern match checking does not understand
ValueEnum
which may result in both lack of correct warnings and incorrect warnings - each enum constant can't have its own API (unless through implicit classes)