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

Abstract Value Members

  1. abstract def enumCtx: EnumCtx
    Attributes
    protected

Concrete Value Members

  1. final def !=(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  2. final def ##(): Int
    Definition Classes
    AnyRef → Any
  3. final def ==(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  4. final def asInstanceOf[T0]: T0
    Definition Classes
    Any
  5. def clone(): AnyRef
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @native() @throws( ... )
  6. final def eq(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  7. def equals(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  8. def finalize(): Unit
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
  9. final def getClass(): Class[_]
    Definition Classes
    AnyRef → Any
    Annotations
    @native()
  10. def hashCode(): Int
    Definition Classes
    AnyRef → Any
    Annotations
    @native()
  11. final def isInstanceOf[T0]: Boolean
    Definition Classes
    Any
  12. 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
  13. final def ne(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  14. final def notify(): Unit
    Definition Classes
    AnyRef
    Annotations
    @native()
  15. final def notifyAll(): Unit
    Definition Classes
    AnyRef
    Annotations
    @native()
  16. 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.

  17. final def synchronized[T0](arg0: ⇒ T0): T0
    Definition Classes
    AnyRef
  18. def toString(): String
    Definition Classes
    NamedEnum → AnyRef → Any
  19. final def wait(): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  20. final def wait(arg0: Long, arg1: Int): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  21. final def wait(arg0: Long): Unit
    Definition Classes
    AnyRef
    Annotations
    @native() @throws( ... )

Inherited from NamedEnum

Inherited from Serializable

Inherited from Serializable

Inherited from AnyRef

Inherited from Any

Ungrouped