Understanding scala enumerations
the Enumeration
trait has a type member Value
representing the individual elements of the enumeration (it's actually an inner class, but the difference doesn't matter here).
Thus object WeekDay
inherits that type member. The line type WeekDay = Value
is just a type alias. It is useful, because after you import it elsewhere with import WeekDay._
, you can use that type, e.g.:
def isWorkingDay(d: WeekDay) = ! (d == Sat || d == Sun)
Instead, a minimal version would just be:
object WeekDay extends Enumeration {
val Mon, Tue, Wed, Thu, Fri, Sat, Sun = Value
}
and you do not have to import the contents of object WeekDay
, but then you would need to use type WeekDay.Value
and to qualify individual members. So the example would become
def isWorkingDay(d: WeekDay.Value) = ! (d == WeekDay.Sat || d == WeekDay.Sun)
The second question is about the meaning of val Mon, ... = Value
. This is indeed very confusing if you don't look into the implementation of Enumeration
. This is not the assignment of a type! It is instead calling a protected method of the same name, Value
, which returns a concrete instance of type Value
.
It so happens that you can write val a, b, c = foo
in Scala, and for each value a
, b
, and c
the method foo
will be called again and again. Enumeration
uses this trick to increment an internal counter so that each value is individual.
If you open the Scala API docs for Enumeration
and click on Visibility: All
, you will see that method appearing.