Dotty Documentation



Edit this page on GitHub

An enumeration is used to define a type consisting of a set of named values.

enum Color {
  case Red, Green, Blue

This defines a new sealed class, Color, with three values, Color.Red, Color.Green, Color.Blue. The color values are members of Colors companion object.

Parameterized enums

Enums can be parameterized.

enum Color(val rgb: Int) {
  case Red   extends Color(0xFF0000)
  case Green extends Color(0x00FF00)
  case Blue  extends Color(0x0000FF)

As the example shows, you can define the parameter value by using an explicit extends clause.

Methods defined for enums

The values of an enum correspond to unique integers. The integer associated with an enum value is returned by its enumTag method:

scala> val red = Color.Red
val red: Color = Red
scala> red.enumTag
val res0: Int = 0

The companion object of an enum also defines three utility methods. The enumValue and enumValueNamed methods obtain an enum value by its tag or its name. The enumValues method returns all enum values defined in an enumeration in an Iterable.

scala> Color.enumValue(1)
val res1: Color = Green
scala> Color.enumValueNamed("Blue")
val res2: Color = Blue
scala> Color.enumValues
val res3: collection.Iterable[Color] = MapLike(Red, Green, Blue)

User-defined members of enums

It is possible to add your own definitions to an enum. Example:

enum Planet(mass: Double, radius: Double) {
  private final val G = 6.67300E-11
  def surfaceGravity = G * mass / (radius * radius)
  def surfaceWeight(otherMass: Double) =  otherMass * surfaceGravity

  case Mercury extends Planet(3.303e+23, 2.4397e6)
  case Venus   extends Planet(4.869e+24, 6.0518e6)
  case Earth   extends Planet(5.976e+24, 6.37814e6)
  case Mars    extends Planet(6.421e+23, 3.3972e6)
  case Jupiter extends Planet(1.9e+27,   7.1492e7)
  case Saturn  extends Planet(5.688e+26, 6.0268e7)
  case Uranus  extends Planet(8.686e+25, 2.5559e7)
  case Neptune extends Planet(1.024e+26, 2.4746e7)

It is also possible to define an explicit companion object for an enum:

object Planet {
  def main(args: Array[String]) = {
    val earthWeight = args(0).toDouble
    val mass = earthWeight / Earth.surfaceGravity
    for (p <- enumValues)
      println(s"Your weight on $p is ${p.surfaceWeight(mass)}")


Enums are represented as sealed classes that extend the scala.Enum trait. This trait defines a single method, enumTag:

package scala

/** A base trait of all enum classes */
trait Enum {

  /** A number uniquely identifying a case of an enum */
  def enumTag: Int

Enum values with extends clauses get expanded to anonymous class instances. For instance, the Venus value above would be defined like this:

val Venus: Planet =
  new Planet(4.869E24, 6051800.0) {
    def enumTag: Int = 1
    override def toString: String = "Venus"
    // internal code to register value

Enum values without extends clauses all share a single implementation that can be instantiated using a private method that takes a tag and a name as arguments. For instance, the first definition of value Color.Red above would expand to:

val Red: Color = $new(0, "Red")


For more info, see Issue #1970 and PR #4003.