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
}
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