Brief introduction
There is no enumeration type in Scala, but the enumeration class is provided in the standard class library to produce enumerations. After extending the enumeration class, call the value method to initialize the possible values in the enumeration.
The inner class value is actually an abstract class, and the real creation is Val. Because it's actually Val, you can pass in the ID and name for value
If not specified, the ID is added to the previous enumeration value ID, and name is the field name
Scala enumeration Example
Object trafficlightcolor extends enumeration { type trafficlightcolor = value val red = value (0, "Stop") val yellow = value ( val green = value) ("Go")}object margin extends enumeration { type margin = value val top, bottom, Left, right = value}import test. Trafficlightcolor._import test. Margin._object driver extends app { println (bottom, bottom.id) Def dowhat (Color: trafficlightcolor) = { if (color == red) "Stop" else if (color == yellow) "hurry Up else go } //use match match def dowhat2 (color: Trafficlightcolor) = color match { case red => "Stop" case yellow => "Hurry up" case _ = > "Go" } // load Red val red = Trafficlightcolor (0) // calls enumeration.apply println (red, red.id) println (Dowhat (red)) println (DOWHAT2 (trafficlightcolor.yellow)) //Print out all enumerations margin.values.foreach { v => println (V,v.id)}}
Scala. Enumeration enumeration Example