Enumeration in Scala

An enumerations serve the purpose of representing a group of named constants in a programming language. Refer Enumeration (or enum) in C and enum in Java for information on enumerations. Scala provides an Enumeration class which we can extend in order to create our enumerations.

Declaration of enumerations in Scala

filter_none

edit
close

play_arrow

link
brightness_4
code

// A simple scala program of enumeration 
  
// Creating enumeration
object Main extends Enumeration 
{
    type Main = Value
      
    // Assigning values
    val first = Value("Thriller")
    val second = Value("Horror")
    val third = Value("Comedy")
    val fourth = Value("Romance")
      
    // Main method
    def main(args: Array[String])
    {
        println(s"Main Movie Genres = ${Main.values}")
    }
}

chevron_right


Output:

Main Movie Genres = Movies.ValueSet(Thriller, Horror, Comedy, Romance)

 
Important points of enum :

  • In Scala, there is no enum keyword unlike Java or C.
  • Scala provides an Enumeration class which we can extend in order to create our enumerations.
  • Every Enumeration constant represents an object of type Enumeration.
  • Enumeration values are defined as val members of the evaluation.
  • When we extended the Enumeration class, a lot of functions get inherited. ID is one among the them.
  • We can iterate the members.

Printing particular element of the enumeration

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program Printing particular 
// element of the enumeration
  
// Creating enumeration
object Main extends Enumeration 
{
    type Main = Value
      
    // Assigning values 
    val first = Value("Thriller")
    val second = Value("Horror")
    val third = Value("Comedy")
    val fourth = Value("Romance")
       
    // Mian method 
    def main(args: Array[String]) 
    {
        println(s"The third value = ${Main.third}")
    }
}

chevron_right


Output:

The third value = Comedy

In above example, Main.third is printing particular element of the enumeration.
 

Printing default ID of any element in the enumeration

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program Printing default ID of 
// any element in the enumeration
  
// Creating Enumeration
object Main extends Enumeration 
{
    type Main = Value
   
    // Assigning Values
    val first = Value("Thriller") // ID = 0
    val second = Value("Horror") // ID = 1
    val third = Value("Comedy") // ID = 2
    val fourth = Value("Romance") // ID = 3
       
    // Main Method 
    def main(args: Array[String]) 
    {
        println(s"ID of third = ${Main.third.id}")
    }
}

chevron_right


Output:

ID of third = 2

In above example, Main.third.id is printing default ID of any element in the enumeration.

 

Matching values in enumeration

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program of Matching values in enumeration
  
// Creating Enumeration
object Main extends Enumeration 
{
    type Main = Value
      
    // Assigning Values
    val first = Value("Thriller")
    val second = Value("Horror")
    val third = Value("Comedy")
    val fourth = Value("Romance")
      
    // Main Method 
    def main(args: Array[String]) 
    {
        Main.values.foreach
        {
            // Matching values in Enumeration
            case d if ( d == Main.third ) => 
            println(s"Favourite type of Movie = $d")
            case _ => None
        
    }
}

chevron_right


Output:

Favourite type of Movie = Comedy

 

Changing default IDs of values

The values are printed in the order of the ID set by us.These values of IDs can be any integer .These IDs need not be in any particular order.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program of Changing
// default IDs of values
  
// Creating Enumeration
object Main extends Enumeration 
{
    type Main = Value
  
    // Assigning Values
    val first = Value(0, "Thriller")
    val second = Value(-1, "Horror")
    val third = Value(-3, "Comedy")
    val fourth = Value(4, "Romance"
      
    // Main Method 
    def main(args: Array[String]) 
    {
        println(s" Movie Genres = ${Main.values}")
    }
}

chevron_right


Output:

Movie Genres = Movies.ValueSet(Comedy, Horror, Thriller, Romance)

 
Reference: https://www.scala-lang.org/api/current/scala/Enumeration.html



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.