Home »
Scala
Enumeration in Scala
By IncludeHelp Last updated : November 07, 2024
Scala Enumerations
Enumeration is a feature of Scala that is used to define a group of named constants accessed using the enum name and ids.
The enums in Scala work similar to enum in other programming languages like C/C++, Java. The difference is in creation syntax. In Scala, the enum's are created by extending an abstract class Enumeration which is defined here,
abstract class Enumeration extends Serializable
This is different from classical enum creation syntax where we would use the keyword enum for creating Enumeration.
Syntax
Syntax for Creating Enumeration
object enum_object extends Enumeration {
type enum_object = value
/// Assigning value
val name1 = Value("value")
}
Example of Enumeration
Program to illustrate creation of Enumeration in Scala
// Program to illustrate the creation of Enum in Scala...
object Main extends Enumeration {
type Main = Value
val day1 = Value("Sunday")
val day2 = Value("Monday")
val day3 = Value("Tuesday")
val day4 = Value("Wednesday")
val day5 = Value("Thursday")
val day6 = Value("Friday")
val day7 = Value("Saturday")
def main(args: Array[String]) {
println("Value of Enum : " + Main.values )
}
}
Output:
Value of Enum : Main.ValueSet(Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday)
In the above code, we have created an object name Main which is used to create enumeration which contains days of the week. Then we have printed its value in the main method using the println.
Accessing values of Enumeration Separately
You can access the values of enumerations separately using the name used to assign the value in the enumerator.
Syntax
enumObj.valName
Program to illustrate the accessing of values of Enumeration
// Program to illustrate the access to values of Enum in Scala...
object Main extends Enumeration {
type Main = Value
val day1 = Value("Sunday")
val day2 = Value("Monday")
val day3 = Value("Tuesday")
val day4 = Value("Wednesday")
val day5 = Value("Thursday")
val day6 = Value("Friday")
val day7 = Value("Saturday")
def main(args: Array[String]) {
println("Values of Enum : ")
println("Value 1 = " + Main.day1)
println("Value 2 = " + Main.day2)
println("Value 3 = " + Main.day3)
println("Value 4 = " + Main.day4)
println("Value 5 = " + Main.day5)
println("Value 6 = " + Main.day6)
println("Value 7 = " + Main.day7)
}
}
Output:
Values of Enum :
Value 1 = Sunday
Value 2 = Monday
Value 3 = Tuesday
Value 4 = Wednesday
Value 5 = Thursday
Value 6 = Friday
Value 7 = Saturday
Accessing the Ids of the values of Enumeration
You can access the id of a value of enumeration also. Scala provides an id to the values of enumerations other than the name which is assigned by the user. The default id's of values of enumerations start from 0.
Syntax
Syntax to get the id of a value:
enumObj.valName.id
Program to illustrate the access to ids of the value of Enumeration
// Program to illustrate the access to ids of values of Enum in Scala...
object Main extends Enumeration {
type Main = Value
val day1 = Value("Sunday")
val day2 = Value("Monday")
val day3 = Value("Tuesday")
val day4 = Value("Wednesday")
val day5 = Value("Thursday")
val day6 = Value("Friday")
val day7 = Value("Saturday")
def main(args: Array[String]) {
println("All Values and Id's of Enum : ")
println("Value of day1 = " + Main.day1 + " and the id is " + Main.day1.id)
println("Value of day2 = " + Main.day2 + " and the id is " + Main.day2.id)
println("Value of day3 = " + Main.day3 + " and the id is " + Main.day3.id)
println("Value of day4 = " + Main.day4 + " and the id is " + Main.day4.id)
println("Value of day5 = " + Main.day5 + " and the id is " + Main.day5.id)
println("Value of day6 = " + Main.day6 + " and the id is " + Main.day6.id)
println("Value of day7 = " + Main.day7 + " and the id is " + Main.day7.id)
}
}
Output:
All Values and Id's of Enum :
Value of day1 = Sunday and the id is 0
Value of day2 = Monday and the id is 1
Value of day3 = Tuesday and the id is 2
Value of day4 = Wednesday and the id is 3
Value of day5 = Thursday and the id is 4
Value of day6 = Friday and the id is 5
Value of day7 = Saturday and the id is 6
Altering the id's of values in Enumeration
You can also change the id's in the enumeration in Scala. The id's of enumeration in Scala can have any integer value.
Syntax
Syntax for altering the value of Id's:
val enumValName = Value(id, value)
Program to alter the id's of values in Enumeration
// Program to illustrate the alter to ids of values of Enum in Scala...
object Main extends Enumeration {
type Main = Value
val day1 = Value(7, "Sunday")
val day2 = Value(1, "Monday")
val day3 = Value(2, "Tuesday")
val day4 = Value(3, "Wednesday")
val day5 = Value(4, "Thursday")
val day6 = Value(5, "Friday")
val day7 = Value(6, "Saturday")
def main(args: Array[String]) {
println("All Values and Id's of Enum : ")
println("Value of day1 = " + Main.day1 + " and the id is " + Main.day1.id)
println("Value of day2 = " + Main.day2 + " and the id is " + Main.day2.id)
println("Value of day3 = " + Main.day3 + " and the id is " + Main.day3.id)
println("Value of day4 = " + Main.day4 + " and the id is " + Main.day4.id)
println("Value of day5 = " + Main.day5 + " and the id is " + Main.day5.id)
println("Value of day6 = " + Main.day6 + " and the id is " + Main.day6.id)
println("Value of day7 = " + Main.day7 + " and the id is " + Main.day7.id)
}
}
Output:
All Values and Id's of Enum :
Value of day1 = Sunday and the id is 7
Value of day2 = Monday and the id is 1
Value of day3 = Tuesday and the id is 2
Value of day4 = Wednesday and the id is 3
Value of day5 = Thursday and the id is 4
Value of day6 = Friday and the id is 5
Value of day7 = Saturday and the id is 6
Enumeration Methods and Values
List of all values and method defined for Enumeration (members of enumeration class) in Scala:
- apply(x: Int)
- maxId
- toString()
- Values
- withName(str: String)
- valueOrdering extends ordering[Value]