An Enum symbol with a symbol name that can be explicitly set and which may be different than its corresponding Scala case object's name.
Both a IndexedEnumSymbol and a AliasedEnumSymbol.
Both a IndexedEnumSymbol and a AliasedEnumSymbol. Should be used in conjunction with IndexedEnum.
Example usage:
sealed abstract class AliasedIndexed(id: Int, name: String)
extends AliasedIndexedEnumSymbol(id, name)
object AliasedIndexed extends IndexedEnum[AliasedIndexed] {
case object Zero extends AliasedIndexed(0, "zero")
case object One extends AliasedIndexed(1, "one")
}
Provides a convenient way to define "ADT" based Scala enumerations (enumerations defined with case objects extending a sealed trait) while still retaining the ability to:
Provides a convenient way to define "ADT" based Scala enumerations (enumerations defined with case objects extending a sealed trait) while still retaining the ability to:
- Look up all the symbols of the enumeration - Look up a particular enumeration symbol given its string name
Using case objects for enumerations has some important advantages over using scala.Enumeration:
- Exhaustive pattern matching - The types used to declare enumeration symbols are 1st class scala types
Example usage:
sealed trait Direction extends EnumSymbol
object Direction extends Enum[Direction] { case object NORTH extends Direction case object SOUTH extends Direction case object EAST extends Direction case object WEST extends Direction }
scala> Direction.symbols res1: Set[Direction] = Set(NORTH, SOUTH, EAST, WEST)
scala> Direction.withName("SOUTH")
res2: Direction = Direction.SOUTH
An Enum symbol.
An Enum symbol.
See Enum for usage details.
For case objects, the symbol name will be the case object's "simple name" (unqualified Scala type name). For case objects requiring a different symbol name than their Scala name, see AliasedEnumSymbol.
An extension of Enum supporting IndexedEnumSymbol symbols.
An extension of Enum supporting IndexedEnumSymbol symbols.
This is indented to provide functionality very close to that of scala.Enumeration to help ease conversions of types using scala.Enumeration's ids.
Example usage:
sealed abstract class Indexed(id: Int) extends IndexedEnumSymbol(id)
object Indexed extends IndexedEnum[Indexed] {
case object Zero extends Indexed(0)
case object One extends Indexed(1)
}
An IndexedEnum symbol indexed by an Int 'id'.
An IndexedEnum symbol indexed by an Int 'id'.
See IndexedEnum for usage details.
An Enum symbol with a symbol name that can be explicitly set and which may be different than its corresponding Scala case object's name.
Example usage:
abstract class RenamedCompany(currentName: String) extends AliasedEnumSymbol(currentName) object RenamedCompany extends Enum[Aliased] { case object BackRub extends RenamedCompany("Google") case object TypeSafe extends RenamedCompany("Lightbend") }
scala> RenamedCompany.BackRub.name res1: String = "Google"
scala RenamedCompany.withName("Lightbend") res2: RenamedCompany = RenamedCompany.TypeSafe