Scala Enumeration: Enumeration overview

Source: Internet
Author: User

Spark Scala Enumeration


* The set of enumerations in Java cannot be used directly in Scala
* Enumerations in Scala are implemented using lightweight enumeration
* The enumeration in Scala is actually a companion object
* Enumerations in Scala do not have a method override function
* The enumeration in Scala is actually the object of Enumeration.value.

Unlike Java and C + +, Scala does not have enumeration types. However, the Spark Standard class library provides a enumeration type that can be used to generate enumerations, define an object of an enumeration class, and invoke all possible values in the enumeration in the value method, for example:

Object WeekDay extends Enumeration {

Val Mon, Tue, Wed, Thu, Fri, Sat, Sun = Value

Define the specific enumeration instance here

}

Here we define 7 fields: Mon, Tue, Wed, Thu, Fri, Sat, Sun, and then initialize them with value, and the code can be expressed as follows:

Val Mon = Value

Val Tue = Value

Val Wed = Value

Val Thu = Value

Val Fri = Value

Val sat= Value

Val Sun = Value

You can also pass in a id,name to the value method, or two parameters are passed in

Val Mon = Value (0, "meeting")

Val Tue = Value (1)

Val Wed = Value

Val Thu = Value

Val Fri = Value

Val sat= Value ("shopping")

Val Sun = Value

If you do not specify an ID value, the ID is based on the previous enumeration value +1, starting from zero, the default Name field is the field name

You can then use Weekday.mon, Weekday.tue to refer to the enumeration values.

The type of enumeration here is Weekday.value instead of weekday, which represents the object

You can add a type name that defines

Object WeekDay extends Enumeration {

Type WeekDay = Value

This is just to expose the type of enumration.value to the outside world.

Val Mon, Tue, Wed, Thu, Fri, Sat, Sun = Value

Define the specific enumeration instance here

}

Now the enumeration type becomes Weekday.weekday

Methods for referencing enum types:

Import Weekday._

def isworkingday (d:weekday) =! (d = = Sat | | d = = Sun)

Weekday.values filter Isworkingday foreach println//output with syntax sugar

Enumeration values can be called by ID or by name value

println (WeekDay (0)) //Output meeting

println (Weekday.mon) //Direct Fetch enumeration value meeting

println (WeekDay.Mon.id) //Take the enumeration value in the ordinal 0

Number of println (WEEKDAY.MAXID) //enumeration Values 7

println (Weekday.withname ("Meeting")) //Get an enumeration through a string (here is no reflection required) meeting

Further understanding the construction and use of enumeration types through the following code

Example:

————————————— define the Trafficlightcolor object —————————————-

Object Trafficlightcolor extends Enumeration {

Type Trafficlightcolor = Value

Val Red = Value (0, "Stop")

Val Yellow = Value (10)

Val Green = Value ("Go")

}

———————— define margin objects and margin. Margin Enumeration Type ———————

Object Margin extends Enumeration {

Type Margin = Value

Val TOP, BOTTOM, left, right = Value

}

———————————————— How to use ——————————————————

Import Trafficlightcolor.trafficlightcolor

Object Driver extends App {

println (Margin.bottom, Margin.BOTTOM.id)

Output (bottom,1)

def dowhat (color:trafficlightcolor) = {

if (color = = trafficlightcolor.red) "Stop"

else if (color = = Trafficlightcolor.yellow) "Hurry up" Else "go"

}

Using match matching

def doWhat2 (color:trafficlightcolor) = Color Match {

Case trafficlightcolor.red = "Stop"

Case Trafficlightcolor.yellow = "Hurry Up"

Case _ = "Go"

}

Load Red

Val red = trafficlightcolor (0)//Calls enumeration.apply

println (red, red.id)

Output: (stop,0)

println (Dowhat (red))

Output: Stop

println (DOWHAT2 (Trafficlightcolor.yellow))

Output: Hurry up

Print out all enumerations

Margin.values.foreach {v = println (v,v.id)}

Output: (top,0)

(bottom,1)

(left,2)

(right,3)

}

Source: Https://github.com/scala/scala/blob/v2.12.0/src/library/scala/Enumeration.scala

Scala Enumeration: Enumeration overview

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.