trait ValueEnum extends NamedEnum
Base trait for val
-based enums, i.e. enums implemented as a single class with companion object keeping
enum values as instances of the enum class in final val
fields. This is an alternative way of implementing
enums as compared to traditional Scala approach of using a sealed hierarchy with objects representing enum values.
Advantages of value based enums over object based enums include:
- Much less classes generated, which in particular contributes to much less JavaScript output in ScalaJS. This may also speed up compilation.
- No need to explicitly implement
values
in enum's companion object as it is necessary when using SealedEnumCompanion and NamedEnumCompanion - It is possible to define all enum values in a single line of code (assuming they don't take parameters)
Disadvantages of value based enums over object based enums include:
- Every object can have its own separate public API, values cannot (although you can have a separate anonymous class for every value)
- Scala compiler does not perform exhaustive checking for pattern matching enum values - this is however
provided separately by
commons-analyzer
compiler plugin.
Enum classes must have a companion object which extends ValueEnumCompanion (prefer using
AbstractValueEnumCompanion where possible). Every enum constant must be declared as a final val
in the
companion object and must have the Value
type explicitly ascribed (which is just a type alias for enum class itself).
The enum class itself must take an implicit EnumCtx argument which provides information about
enum ordinal and name as well as makes sure that enum value is registered in the companion object.
If possible, you should always extend AbstractValueEnum instead of mixing in this trait.
ValueEnum trait should only be mixed in directly when your enum class already has another superclass,
incompatible with AbstractValueEnum.
final class Weekday(implicit enumCtx: EnumCtx) extends AbstractValueEnum object Weekday extends AbstractValueEnumCompanion[Weekday] { final val Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday: Value = new Weekday }
Value based enums can take parameters:
final class Status(val description: String)(implicit enumCtx: EnumCtx) extends AbstractValueEnum object Status extends AbstractValueEnumCompanion[Status] { final val Enabled: Value = new Status("Something is enabled and working") final val Disabled: Value = new Status("Something is disabled and not working") }
- Alphabetic
- By Inheritance
- ValueEnum
- NamedEnum
- Serializable
- Serializable
- AnyRef
- Any
- Hide All
- Show All
- Public
- All
Concrete Value Members
-
final
def
!=(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
final
def
##(): Int
- Definition Classes
- AnyRef → Any
-
final
def
==(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
final
def
asInstanceOf[T0]: T0
- Definition Classes
- Any
-
def
clone(): AnyRef
- Attributes
- protected[java.lang]
- Definition Classes
- AnyRef
- Annotations
- @native() @throws( ... )
-
final
def
eq(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
-
def
equals(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
def
finalize(): Unit
- Attributes
- protected[java.lang]
- Definition Classes
- AnyRef
- Annotations
- @throws( classOf[java.lang.Throwable] )
-
final
def
getClass(): Class[_]
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
-
def
hashCode(): Int
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
-
final
def
isInstanceOf[T0]: Boolean
- Definition Classes
- Any
-
def
name: String
Name of the
final val
in enum companion object that this enum value is assigned to. -
final
def
ne(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
-
final
def
notify(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
-
final
def
notifyAll(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
-
def
ordinal: Int
Enum value index, starting from 0.
Enum value index, starting from 0. Reflects the order in which enum constants are declared in the companion object of the enum class.
-
final
def
synchronized[T0](arg0: ⇒ T0): T0
- Definition Classes
- AnyRef
-
def
toString(): String
- Definition Classes
- NamedEnum → AnyRef → Any
-
final
def
wait(): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
-
final
def
wait(arg0: Long, arg1: Int): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
-
final
def
wait(arg0: Long): Unit
- Definition Classes
- AnyRef
- Annotations
- @native() @throws( ... )