|
| 1 | +# Enums |
| 2 | + |
| 3 | +The most basic use of enums is as enumerations of simple values: |
| 4 | + |
| 5 | +```scala |
| 6 | +enum Color { |
| 7 | + case Red, Green, Blue |
| 8 | +} |
| 9 | +``` |
| 10 | + |
| 11 | +This defines a new class, `Color`, with three values, `Color.Red`, |
| 12 | +`Color.Green`, `Color.Blue`. The color values are members of `Color`s |
| 13 | +companion object. The `Color` definition above is equivalent to the |
| 14 | +following more explicit definition of an _enum class_ and a companion |
| 15 | +object: |
| 16 | + |
| 17 | +```scala |
| 18 | +enum class Color |
| 19 | +object Color { |
| 20 | + case Red |
| 21 | + case Green |
| 22 | + case Blue |
| 23 | +} |
| 24 | +``` |
| 25 | + |
| 26 | +## Parameterized enums |
| 27 | + |
| 28 | +Enum classes can be parameterized. |
| 29 | + |
| 30 | +```scala |
| 31 | +enum Color(val rgb: Int) { |
| 32 | + case Red extends Color(0xFF0000) |
| 33 | + case Green extends Color(0x00FF00) |
| 34 | + case Blue extends Color(0x0000FF) |
| 35 | +} |
| 36 | +``` |
| 37 | + |
| 38 | +As the example shows, you can define the parameter value by using an |
| 39 | +explicit extends clause. |
| 40 | + |
| 41 | +## Methods defined for enums |
| 42 | + |
| 43 | +The values of an enum correspond to unique integers. The integer |
| 44 | +associated with an enum value is returned by its `enumTag` method: |
| 45 | + |
| 46 | +```scala |
| 47 | +scala> val red = Color.Red |
| 48 | +val red: Color = Red |
| 49 | +scala> red.enumTag |
| 50 | +val res0: Int = 0 |
| 51 | +``` |
| 52 | + |
| 53 | +The companion object of an enum class also defines three utility methods. |
| 54 | +The `enumValue` and `enumValueNamed` methods obtain an enum value |
| 55 | +by its tag or its name. The `enumValues` method returns all enum values |
| 56 | +defined in an enumeration in an `Iterable`. |
| 57 | + |
| 58 | +```scala |
| 59 | +scala> Color.enumValue(1) |
| 60 | +val res1: Color = Green |
| 61 | +scala> Color.enumValueNamed("Blue") |
| 62 | +val res2: Color = Blue |
| 63 | +scala> Color.enumValues |
| 64 | +val res3: collection.Iterable[Color] = MapLike(Red, Green, Blue) |
| 65 | +``` |
| 66 | + |
| 67 | +## User-defined members of enums |
| 68 | + |
| 69 | +It is possible to add your own definitions to an enum class or its |
| 70 | +companion object. To make clear what goes where you need to use the |
| 71 | +longer syntax which defines an enum class alongside its companion |
| 72 | +object explicitly. In the following example, we define some methods in |
| 73 | +class `Planet` and a `main` method in its companion object. |
| 74 | + |
| 75 | +```scala |
| 76 | +enum class Planet(mass: Double, radius: Double) { |
| 77 | + private final val G = 6.67300E-11 |
| 78 | + def surfaceGravity = G * mass / (radius * radius) |
| 79 | + def surfaceWeight(otherMass: Double) = otherMass * surfaceGravity |
| 80 | +} |
| 81 | + |
| 82 | +object Planet { |
| 83 | + case MERCURY extends Planet(3.303e+23, 2.4397e6) |
| 84 | + case VENUS extends Planet(4.869e+24, 6.0518e6) |
| 85 | + case EARTH extends Planet(5.976e+24, 6.37814e6) |
| 86 | + case MARS extends Planet(6.421e+23, 3.3972e6) |
| 87 | + case JUPITER extends Planet(1.9e+27, 7.1492e7) |
| 88 | + case SATURN extends Planet(5.688e+26, 6.0268e7) |
| 89 | + case URANUS extends Planet(8.686e+25, 2.5559e7) |
| 90 | + case NEPTUNE extends Planet(1.024e+26, 2.4746e7) |
| 91 | + |
| 92 | + def main(args: Array[String]) = { |
| 93 | + val earthWeight = args(0).toDouble |
| 94 | + val mass = earthWeight/EARTH.surfaceGravity |
| 95 | + for (p <- enumValues) |
| 96 | + println(s"Your weight on $p is ${p.surfaceWeight(mass)}") |
| 97 | + } |
| 98 | +} |
| 99 | +``` |
| 100 | + |
| 101 | +## Implementation |
| 102 | + |
| 103 | +Enum values with `extends` clauses get expanded to anonymus class instances. |
| 104 | +For instance, the `VENUS` value above would be defined like this: |
| 105 | + |
| 106 | +```scala |
| 107 | +val VENUS: Planet = |
| 108 | + new Planet(4.869E24, 6051800.0) { |
| 109 | + def enumTag: Int = 1 |
| 110 | + override def toString: String = "VENUS" |
| 111 | + // internal code to register value |
| 112 | + } |
| 113 | +``` |
| 114 | + |
| 115 | +Enum values without `extends` clauses all share a single implementation |
| 116 | +that can be instantiated using a private method that takes a tag and a name as arguments. |
| 117 | +For instance, the first |
| 118 | +definition of value `Color.Red` above would expand to: |
| 119 | + |
| 120 | +```scala |
| 121 | +val Red: Color = $new(0, "Red") |
| 122 | +``` |
0 commit comments