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 Value type member of the enumeration (Value selected on the stable identifier path of the enumeration instance).
Values SHOULD NOT be added to an enumeration after its construction; doing so makes the enumeration thread-unsafe. If values are added to an enumeration from multiple threads (in a non-synchronized fashion) after construction, the behavior of the enumeration is undefined.
| Value parameters |
|
|---|---|
| Example |
// Define a new enumeration with a type alias and work with the full set of enumerated values
object WeekDay extends Enumeration {
type WeekDay = Value
val Mon, Tue, Wed, Thu, Fri, Sat, Sun = Value
}
import WeekDay._
def isWorkingDay(d: WeekDay) = ! (d == Sat || d == Sun)
WeekDay.values filter isWorkingDay foreach println
// output:
// Mon
// Tue
// Wed
// Thu
// Fri
// 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)
|
| Supertypes | |
| Known subtypes | |
| Self type |
A class implementing the scala.Enumeration.Value type. This class can be overridden to change the enumeration's naming and integer identification behaviour.
The type of the enumerated values.
An ordering by id for values of this set
A class for sets of values. Iterating through this set will yield values in increasing order of their ids.
| Value parameters |
|
|---|---|
| Companion | object |
A factory object for value sets
| Companion | class |
|---|
Creates a fresh value, part of this enumeration.
Creates a fresh value, part of this enumeration, identified by the integer i.
| Value parameters |
|
|---|---|
| Returns | Fresh value identified by |
Creates a fresh value, part of this enumeration, called name.
| Value parameters |
|
|---|---|
| Returns | Fresh value called |
Creates a fresh value, part of this enumeration, called name and identified by the integer i.
| Value parameters |
|
|---|---|
| Returns | Fresh value with the provided identifier |
The value of this enumeration with given id x
The one higher than the highest integer amongst those used to identify values in this enumeration.
The name of this enumeration.
| Definition Classes | Any |
|---|
The values of this enumeration as a set.
Return a Value from this Enumeration whose name matches the argument s. The names are determined automatically via reflection.
| Value parameters |
|
|---|---|
| Returns | the |
| Throws |
|
The integer to use to identify the next created value.
The string to use to name the next created value.
© 2002-2022 EPFL, with contributions from Lightbend.
Licensed under the Apache License, Version 2.0.
https://scala-lang.org/api/3.2.0/scala/Enumeration.html