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.

Example:
  1. 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")
    }
Linear Supertypes
NamedEnum, Serializable, Serializable, AnyRef, Any
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. ValueEnum
  2. NamedEnum
  3. Serializable
  4. Serializable
  5. AnyRef
  6. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. All

Concrete Value Members

  1. def name: String

    Name of the final val in enum companion object that this enum value is assigned to.

    Name of the final val in enum companion object that this enum value is assigned to.

    Definition Classes
    ValueEnumNamedEnum
  2. 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.

  3. def toString(): String
    Definition Classes
    NamedEnum → AnyRef → Any