Trait/Object

com.avsystem.commons.misc

ValueEnum

Related Docs: object ValueEnum | package misc

Permalink

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:

Disadvantages of value based enums over object based enums include:

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. 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
Known Subclasses
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

    Permalink
    Attributes
    protected

Concrete Value Members

  1. final def !=(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  2. final def ##(): Int

    Permalink
    Definition Classes
    AnyRef → Any
  3. final def ==(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  4. final def asInstanceOf[T0]: T0

    Permalink
    Definition Classes
    Any
  5. def clone(): AnyRef

    Permalink
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  6. final def eq(arg0: AnyRef): Boolean

    Permalink
    Definition Classes
    AnyRef
  7. def equals(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  8. def finalize(): Unit

    Permalink
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
  9. final def getClass(): Class[_]

    Permalink
    Definition Classes
    AnyRef → Any
  10. def hashCode(): Int

    Permalink
    Definition Classes
    AnyRef → Any
  11. final def isInstanceOf[T0]: Boolean

    Permalink
    Definition Classes
    Any
  12. def name: String

    Permalink

    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

    Permalink
    Definition Classes
    AnyRef
  14. final def notify(): Unit

    Permalink
    Definition Classes
    AnyRef
  15. final def notifyAll(): Unit

    Permalink
    Definition Classes
    AnyRef
  16. def ordinal: Int

    Permalink

    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

    Permalink
    Definition Classes
    AnyRef
  18. def toString(): String

    Permalink
    Definition Classes
    NamedEnum → AnyRef → Any
  19. final def wait(): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  20. final def wait(arg0: Long, arg1: Int): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  21. final def wait(arg0: Long): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )

Inherited from NamedEnum

Inherited from Serializable

Inherited from Serializable

Inherited from AnyRef

Inherited from Any

Ungrouped