published on in intro

Enums in Scala

Enumerations in Scala

Generally in programming, enumerations (a.k.a. enums) are light-weight means to create a very simple model within our code, for representing a custom type with a limited set of possible values with readable names. This description might sound a bit too abstract, so let’s see an example instead.

Example: days of week

Let’s introduce an enum for representing the days of week. In Scala, you have to create an object for this, which needs to extend Scala’s Enumeration trait (we’ll learn more about objects and traits later).

object DayOfWeek extends Enumeration {
  val Sun, Mon, Wed, Thu, Fri, Sat = Value
}

As you can see, it’s almost as easy as listing the possible values, separated by commas. But don’t forget the “val” from the beginning and the “ = Value” part from the end of the value list, because without them, your shiny new enum will not work.

After defining this new enum in our code we can start using days of week (after importing them!) as in the following example:

import DayOfWeek._

override def main(args: Array[String]): Unit = {
  println(Mon)
}

And this tiny program will print the string “Mon” to the screen. Try it!

It’s also easy to loop over all possible values of an enum:

override def main(args: Array[String]): Unit = {
  DayOfWeek.values.foreach { day => println(day) }
}

That’s it!

In the next post, we’ll see how to use enum values when using pattern matching in Scala.