Scala Tutorial
Basics
Control Statements
OOP Concepts
Parameterized - Type
Exceptions
Scala Annotation
Methods
String
Scala Packages
Scala Trait
Collections
Scala Options
Miscellaneous Topics
In Scala, you can use the Enumeration
class to create a set of named values, similar to enums in other languages. Here's how you can define and use enumerations in Scala:
To define an enumeration, you extend the Enumeration
class and create Value
instances for each enumeration item:
object Colors extends Enumeration { type Colors = Value val Red, Green, Blue = Value }
Here, Colors
is an object that contains three named values: Red
, Green
, and Blue
.
You can access the values of an enumeration using the names you've provided:
println(Colors.Red) // Outputs: Red println(Colors.Green) // Outputs: Green println(Colors.Blue) // Outputs: Blue
You can assign custom IDs and names to enumeration values:
object Days extends Enumeration { val Sunday = Value(0, "SUN") val Monday = Value(1, "MON") val Tuesday = Value(2, "TUE") // ... and so on }
You can iterate over all values in an enumeration using the values
method:
for (color <- Colors.values) { println(color) }
Enumerations can also be used in pattern matching:
def describeColor(color: Colors.Colors): String = color match { case Colors.Red => "The color of passion." case Colors.Green => "The color of nature." case Colors.Blue => "The color of the sky." case _ => "Unknown color." } println(describeColor(Colors.Green)) // Outputs: The color of nature.
However, there are some limitations and criticisms of Scala's built-in Enumeration
, like lack of ability to add methods per enum item or having exhaustive pattern matching at compile-time. Due to these reasons, many Scala developers also opt to use sealed trait/class hierarchies or third-party libraries for more complex enum scenarios.
Scala Enumeration vs case objects:
Enumeration
and case objects
. Enumeration
is a trait, while case objects
are case classes without parameters.object Weekday extends Enumeration { val Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday = Value }
sealed trait Weekday case object Monday extends Weekday case object Tuesday extends Weekday // ... other weekdays
Creating enumerations in Scala:
Enumeration
trait or by using a sealed trait with case objects.object Direction extends Enumeration { val North, East, South, West = Value }
sealed trait Direction case object North extends Direction case object East extends Direction // ... other directions
Enumeration with values in Scala:
Enumeration
, each entry is assigned an incrementing integer by default.object Priority extends Enumeration { val Low = Value(1) val Medium = Value(2) val High = Value(3) }
sealed trait Priority case object Low extends Priority case object Medium extends Priority case object High extends Priority
Iterating over enumeration values in Scala:
values
method.for (day <- Weekday.values) { println(day) }
val weekdays: List[Weekday] = List(Monday, Tuesday, Wednesday, Thursday, Friday) for (day <- weekdays) { println(day) }
Pattern matching with Scala enumerations:
val day: Weekday.Value = Weekday.Monday val result = day match { case Weekday.Saturday | Weekday.Sunday => "Weekend" case _ => "Weekday" }
val direction: Direction = North val result = direction match { case North | South => "Vertical" case East | West => "Horizontal" }
Scala Enumeration withName method:
withName
method in enumerations allows obtaining an enumeration value by its string representation.val dayString = "Monday" val day: Weekday.Value = Weekday.withName(dayString)
val directionString = "North" val direction: Direction = Direction.values.find(_.toString == directionString).getOrElse(DefaultDirection)
Enumeration in Scala collections:
val allDays: Seq[Weekday.Value] = Weekday.values.toSeq
val someDirections: List[Direction] = List(North, East, West)
Using Scala enumerations in pattern matching:
def describeDay(day: Weekday.Value): String = day match { case Weekday.Saturday | Weekday.Sunday => "Weekend" case _ => "Weekday" }
def describeDirection(direction: Direction): String = direction match { case North | South => "Vertical" case East | West => "Horizontal" }
Enumeration and sealed traits in Scala:
sealed trait Status case object Success extends Status case object Failure extends Status
Scala Enumeration and type safety:
val day: Weekday.Value = Weekday.Monday // Type-safe
val direction: Direction = North // Type-safe
Enumerations in Scala standard library:
scala.concurrent.ExecutionContext.Implicits
.import scala.concurrent.ExecutionContext.Implicits.global