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
Scala
// 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}")
}
}
OutputMain 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
Scala
// 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")
// Main method
def main(args: Array[String])
{
println(s"The third value = ${Main.third}")
}
}
OutputThe third value = Comedy
In above example, Main.third is printing particular element of the enumeration.
Printing default ID of any element in the enumeration
Scala
// 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}")
}
}
In above example, Main.third.id is printing default ID of any element in the enumeration.
Matching values in enumeration
Scala
// 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
}
}
}
OutputFavourite 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.
Scala
// 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}")
}
}
OutputMovie Genres = Movies.ValueSet(Comedy, Horror, Thriller, Romance)
Reference: https://www.scala-lang.org/api/current/scala/Enumeration.html
Similar Reads
Trait Linearization in Scala Scala Linearization is a deterministic process which comes into play when an object of a class is created which is defined using inheritance of different traits and classes. linearization helps to resolve the diamond problem which occurs when a class or trait inherits a same property from 2 differen
5 min read
Scala Programming Language Scala is a general-purpose, high-level, multi-paradigm programming language. It is a pure object-oriented programming language which also provides support to the functional programming approach. Scala programs can convert to bytecodes and can run on the JVM (Java Virtual Machine). Scala stands for S
3 min read
Scala Map Map is a collection of key-value pairs. In other words, it is similar to dictionary. Keys are always unique while values need not be unique. Key-value pairs can have any data type. However, data type once used for any key and value must be consistent throughout. Maps are classified into two types: m
5 min read
Interesting fact about Scala Scala(pronounced as "skah-lah") is general-purpose programming language designed by Martin Odersky. The design of Scala started in 2001 at EPFL, Lausanne, Switzerland. Scala was released publicly in 2004 on Java platform. Scala is designed to be concise and addresses criticisms of Java. Scala source
3 min read
Scala | Methods to Call Option The Option in Scala is referred to a carrier of single or no element for a stated type. When a method returns a value which can even be null then Option is utilized i.e, the method defined returns an instance of an Option, in place of returning a single object or a null. There are a few methods that
5 min read
Difference between Case Objects vs Enumerations in Scala Scala offers multiple constructs for representing a fixed set of values, among which case objects and enumerations stand out. While both serve similar purposes, they have distinct characteristics and use cases. In this article, we'll explore the differences between case objects and enumerations in S
4 min read