How to create and use Scala enumerations

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 10.26, “How to Create and Use Scala Enumerations”

Problem

You want to use an enumeration (a set of named values that act as constants) in your application.

Solution

Extend the scala.Enumeration class to create your enumeration:

package com.acme.app {
    object Margin extends Enumeration {
        type Margin = Value
        val TOP, BOTTOM, LEFT, RIGHT = Value
    }
}

Then import the enumeration to use it in your application:

object Main extends App {
    import com.acme.app.Margin._

    // use an enumeration value in a test
    var currentMargin = TOP

    // later in the code ...
    if (currentMargin == TOP) println("working on Top")

    // print all the enumeration values
    import com.acme.app.Margin
    Margin.values foreach println
}

Enumerations are useful tool for creating groups of constants, such as days of the week, weeks of the year, and many other situations where you have a group of related, constant values.

Scala Enumeration sort order

A nice feature of the Enumeration class is that it uses the order in which you define members as the sort order for each element in the enumeration. What I mean by this is that if you define an enumeration like this to define a deck of cards:

object FaceValue extends Enumeration {
    type FaceValue = Value
    val Ace, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten,
        Jack, Queen, King = Value
}

and then print the elements like this:

object Test extends App {

    import FaceValue._

    // see that they're printed in order
    FaceValue.values foreach println
    
    // test less-than relationships
    if (Ace   < Two)   println("Ace   < Two")
    if (Two   < Three) println("Two   < Three")
    if (Three < Four)  println("Three < Four")
    if (Four  < Five)  println("Four  < Five")
    if (Five  < Six)   println("Five  < Six")
    if (Six   < Seven) println("Six   < Seven")
    if (Seven < Eight) println("Seven < Eight")
    if (Eight < Nine)  println("Eight < Nine")
    if (Nine  < Ten)   println("Nine  < Ten")
    if (Ten   < Jack)  println("Ten   < Jack")
    if (Jack  < Queen) println("Jack  < Queen")
    if (Queen < King)  println("Queen < King")

}

you’ll see this output:

Ace
Two
Three
Four
Five
Six
Seven
Eight
Nine
Ten
Jack
Queen
King

Ace   < Two
Two   < Three
Three < Four
Four  < Five
Five  < Six
Six   < Seven
Seven < Eight
Eight < Nine
Nine  < Ten
Ten   < Jack
Jack  < Queen
Queen < King

I find that to be a useful (essential?) trait of enumerations.

Using traits and case objects for enumerations

You can also use the following approach, but it generates about four times as much code as an Enumeration, most of which you won’t need if your sole purpose is to use it like an enumeration:

// a "heavier" approach
package com.acme.app {
    trait Margin
    case object TOP extends Margin
    case object RIGHT extends Margin
    case object BOTTOM extends Margin
    case object LEFT extends Margin
}

See Also