How to use enumeration in Scala



Enumerations are usually used to define constants of known quantities, such as months, weeks, seasons, etc. People who have used java know that the keyword for defining enumerations is enum, which is different from java in scala. Let's see a complete Example definition:



object EnumTest {

  /***
    * Define an enumeration for a week
    */
  object WeekDay extends Enumeration{
    type WeekDay = Value //Declare the type of variables exposed by the enumeration
    val Mon = Value("1")
    val Tue = Value("2")
    val Wed = Value("3")
    val Thu = Value("4")
    val Fri = Value("5")
    val Sat = Value("6")
    val Sun = Value("7")
    def checkExists(day:String) = this.values.exists(_.toString==day) //Check whether this enumeration value exists
    def isWorkingDay(day:WeekDay) = ! ( day==Sat || day == Sun) //Determine whether it is a working day
    def showAll = this.values.foreach(println) // print all enumeration values
  }



  def main(args: Array[String]): Unit = {

    println(WeekDay.checkExists("8"))//Check if it exists

    println(WeekDay.Sun==WeekDay.withName("7"))//Correct usage

    println(WeekDay.Sun=="7")//Wrong usage

    WeekDay.showAll //Print all enumeration values

    println(WeekDay.isWorkingDay(WeekDay.Sun)) //Whether it is a working day

  }

}






The above example basically covers all the common methods of enumeration. To define enumeration, we inherit the Enumeration abstract class, and then assign the value of each enumeration through the internal object Value. In addition, it also defines several tool methods, such as Determine whether there is an enumeration, whether it is a working day, and print all enumeration values, which can be accessed directly through Object when used.


You can also use enumeration values ​​for pattern matching:

    val weekDay=WeekDay.Sun

    weekDay match {
      case WeekDay.Mon=> println("Monday")
      case WeekDay.Tue=> println("Tuesday")
      case WeekDay.Wed=> println("Wednesday")
      case WeekDay.Thu=> println("Thursday")
      case WeekDay.Fri=> println("Friday")
      case WeekDay.Sat=> println("Saturday")
      case WeekDay.Sun=> println("Sunday")

    }
    
    //Sunday



The above is basically the most common enumeration method in Scala. Of course, this common enumeration has some disadvantages, such as the pattern matching above. If I only write 3 enumeration values, it can still pass at compile time, but in An error will be reported when running. In the end, this method cannot enumerate an object, but a simple type. If the value to be enumerated is itself a Bean, then another method is required, which will not be expanded here. Interested Friends can refer to the following link:

http://pedrorijo.com/blog/scala-enums/

If you have any questions, you can scan the code and follow the WeChat public account: I am the siege division (woshigcs), leave a message in the background for consultation. Technical debts cannot be owed, and health debts cannot be owed. On the road of seeking the Tao, walk with you.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326942333&siteId=291194637