Brief introduction
There are no enumeration types 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 class 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 is 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
Scala enumeration Example Object Trafficlightcolor extends enumeration { type Trafficlightcolor = value val Red = value (0, "Stop") val Yellow = value (Ten) val Green = value ("Go")}object margin extends enumeration { type margin = value VA L 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:traff Iclightcolor) = 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 (GO)