abstract class Enum extends Serializable
Defines a finite set of values specific to the enumeration. Typically these values enumerate all possible forms something can take and provide a lightweight alternative to case classes.
Each call to a Value
method adds a new unique value to the enumeration.
To be accessible, these values are usually defined as val
members of
the enumeration.
All values in an enumeration share a common, unique type defined as the
abstract Value
type member of the enumeration (Value
selected on the
stable identifier path of the enumeration instance).
Besides, in contrast to Scala's built-in Enumeration, the Value
type
member can be extended in subclasses, such that it is possible to mix-in
traits, for example. In this case, make sure to make the constructor of
Value
private. Example:
// adding methods to Value class Day private extends Day.Val { def isWorkingDay: Boolean = this != Day.Saturday && this != Day.Sunday } object Day extends Enum { type Value = Day val Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday = new Day } // usage: Day.values filter (_.isWorkingDay) foreach println // output: // Monday // Tuesday // Wednesday // Thursday // Friday
- Self Type
- Enum
- Annotations
- @SerialVersionUID()
// Example of adding attributes to an enumeration by extending the Enumeration.Val class object Planet extends Enumeration { protected case class PlanetVal(mass: Double, radius: Double) extends super.Val { def surfaceGravity: Double = Planet.G * mass / (radius * radius) def surfaceWeight(otherMass: Double): Double = otherMass * surfaceGravity } import scala.language.implicitConversions implicit def valueToPlanetVal(x: Value): PlanetVal = x.asInstanceOf[PlanetVal] val G: Double = 6.67300E-11 val Mercury = PlanetVal(3.303e+23, 2.4397e6) val Venus = PlanetVal(4.869e+24, 6.0518e6) val Earth = PlanetVal(5.976e+24, 6.37814e6) val Mars = PlanetVal(6.421e+23, 3.3972e6) val Jupiter = PlanetVal(1.9e+27, 7.1492e7) val Saturn = PlanetVal(5.688e+26, 6.0268e7) val Uranus = PlanetVal(8.686e+25, 2.5559e7) val Neptune = PlanetVal(1.024e+26, 2.4746e7) } println(Planet.values.filter(_.radius > 7.0e6)) // output: // Planet.ValueSet(Jupiter, Saturn, Uranus, Neptune)
- Alphabetic
- By Inheritance
- Enum
- Serializable
- AnyRef
- Any
- Hide All
- Show All
- Public
- Protected
Instance Constructors
Type Members
- class Val extends Ordered[Value] with Serializable
The superclass of the
scala.Enum.Value
type.The superclass of the
scala.Enum.Value
type. This class can be overridden to change the enumeration's naming and integer identification behaviour, as well as to add additional public functionality.- Annotations
- @SerialVersionUID()
- abstract type Value <: Val
The type of the enumerated values.
- class ValueSet extends AbstractSet[Value] with SortedSet[Value] with SortedSetOps[Value, SortedSet, ValueSet] with StrictOptimizedIterableOps[Value, Set, ValueSet] with Serializable
A class for sets of values.
A class for sets of values. Iterating through this set will yield values in increasing order of their ids.
- Annotations
- @SerialVersionUID()
Value Members
- final def !=(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
- final def ##: Int
- Definition Classes
- AnyRef → Any
- final def ==(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
- final def apply(x: Int): Value
The value of this enumeration with given id
x
- final def asInstanceOf[T0]: T0
- Definition Classes
- Any
- def clone(): AnyRef
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.CloneNotSupportedException]) @native()
- final def eq(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
- def equals(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef → Any
- def finalize(): Unit
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.Throwable])
- final def getClass(): Class[_ <: AnyRef]
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
- def hashCode(): Int
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
- final def isInstanceOf[T0]: Boolean
- Definition Classes
- Any
- final def maxId: Int
The one higher than the highest integer amongst those used to identify values in this enumeration.
- final def ne(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
- var nextId: Int
The integer to use to identify the next created value.
The integer to use to identify the next created value.
- Attributes
- protected
- var nextName: Iterator[String]
The string to use to name the next created value.
The string to use to name the next created value.
- Attributes
- protected
- final def notify(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
- final def notifyAll(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
- def readResolve(): AnyRef
- Attributes
- protected
- final def synchronized[T0](arg0: => T0): T0
- Definition Classes
- AnyRef
- def toString(): String
The name of this enumeration.
The name of this enumeration.
- Definition Classes
- Enum → AnyRef → Any
- def values: ValueSet
The values of this enumeration as a set.
- final def wait(): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException])
- final def wait(arg0: Long, arg1: Int): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException])
- final def wait(arg0: Long): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException]) @native()
- final def withName(s: String): Value
Return a
Value
from thisEnum
whose name matches the arguments
.Return a
Value
from thisEnum
whose name matches the arguments
. The names are determined automatically via reflection.- s
an
Enum
name- returns
the
Value
of thisEnum
if its name matchess
- Exceptions thrown
NoSuchElementException
if noValue
with a matching name is in thisEnum
- implicit object ValueOrdering extends Ordering[Value]
An ordering by id for values of this set
- object ValueSet extends SpecificIterableFactory[Value, ValueSet] with Serializable
A factory object for value sets
A factory object for value sets
- Annotations
- @SerialVersionUID()