Updated March 30, 2023
Introduction to Scala Enum
Enum stands for Enumeration. Enums is the collection of items that represent some constant and provide light-weighted value when required. We basically use an enum to represent a group of values that fall under the same category. Like weekdays can create an enum or month name can also be able to create an enum in any language. Enum works in the same way as they work in c and java. Scala enum can be implemented by using the Enumeration class. If we want to create, then we have to extend this class.
Syntax
object Enum_name extends Enumeration {
type Enum_name = Value
val constant1, constant2, constant3, and so on ... = Value
}
In scala, we have an enumeration class to create enum. For that, we need to extend this class and give a name to the enum. While defining enum, we need to use type alias as well. After that, we can define values that will be part of the enum. This is the syntax to define an enum.
object MonthName extends Enumeration {
type MonthName = Value
val Jan, Feb, Mar, Apr, May, Jun, Jul Aug, Sep, Oct, Nov, Dec = Value
}
In this, we are trying to create an enum that represents the month name.
How does Enum Function Work in Scala?
Scala enum is worked in the same way as any other programming language. They are basically a set of finite values, and these values are an enumerated form of a defined enum can take. This enum in the future will provide a lightweight alternative value to the requested classes. We can also fetch the value of an enum constant. For this scala, the enum provides methods to work with it.
While defining the value of an enum, we are using the val keyword to make the value easily accessible. Therefore, all the values that we define inside the enum would be representing a common and share a common category.
Lets’s take a look at its supertypes, subclass, and hierarchy;
1) Scala enum supertype class;
- io.Serializable
- Anyref
- Anyref
2) Scala enum has known subclasses;
- RoundingMode
3) Scala enum hierarchy;
- Serializable
- Enumeration
- RoundingMode
Below is one example which will explain its working; what we are doing here is we are creating one enum with values like the name of the day. The first thing we need to keep in mind while creating enum is we have to extend the enumeration class. If we do not do this compile-time error will be generated. Once created we are assigning values to the enum by using the type and Values keyword. After that, we are printing out the values that we have assigned to the constant. For this, we are using the enum name followed by the values method.
object Main extends App{
// Your code here!
var demo = new Demo()
println(s"name of the days are :: = ${demo.values}")
}
class Demo extends Enumeration
{
// creating with type alias
type Demo = Value
// values foe enum
val day1 = Value("Sun")
val day2 = Value("Mon")
val day3 = Value("Tue")
val day4 = Value("Wed")
val day5 = Value("Thu")
val day6 = Value("Fri")
val day7 = Value("Sat")
}
Constructor available inside Enumeration class :
1) new Enumeration(): This is the default constructor with no parameter.
2) new Enumeration(count_val: Int): The parameterised constructor takes one integer parameter. This count value would be the initial values from which the count will be defined for value defines inside enum at run time.
Examples of Scala Enum
Given below are the examples of Scala Enum:
Example #1
In this example, we are just printing out the values that are being assigned to the enum class by using the values method.
Code:
object Main extends App{
// Your code here!
var demo = new Demo()
println(s"name of the days are :: = ${demo.values}")
}
class Demo extends Enumeration
{
// creating with type alias
type Demo = Value
// values foe enum
val day1 = Value("Sun")
val day2 = Value("Mon")
val day3 = Value("Tue")
val day4 = Value("Wed")
val day5 = Value("Thu")
val day6 = Value("Fri")
val day7 = Value("Sat")
}
Output:
Example #2
In this example, we are printing out the value of the constant by using the variable name.
Code:
object Main extends App{
// Your code here!
var demo = new Demo()
println(s"value for day1 is :: = ${demo.day1}")
println(s"value for day2 is :: = ${demo.day2}")
println(s"value for day3 is :: = ${demo.day3}")
println(s"value for day4 is :: = ${demo.day4}")
println(s"value for day5 is :: = ${demo.day5}")
println(s"value for day6 is :: = ${demo.day6}")
println(s"value for day7 is :: = ${demo.day7}")
}
class Demo extends Enumeration
{
// creating with type alias
type Demo = Value
// values foe enum
val day1 = Value("Sun")
val day2 = Value("Mon")
val day3 = Value("Tue")
val day4 = Value("Wed")
val day5 = Value("Thu")
val day6 = Value("Fri")
val day7 = Value("Sat")
}
Output:
Example #3
In this example, we are printing out the initial value that is being assigned to each constant of the enum b using the .id method.
Code:
object Main extends App{
// Your code here!
var demo = new Demo()
println(s"id for day1 is :: = ${demo.day1.id}")
println(s"id for day2 is :: = ${demo.day2.id}")
println(s"id for day3 is :: = ${demo.day3.id}")
println(s"id for day4 is :: = ${demo.day4.id}")
println(s"id for day5 is :: = ${demo.day5.id}")
println(s"id for day6 is :: = ${demo.day6.id}")
println(s"id for day7 is :: = ${demo.day7.id}")
}
class Demo extends Enumeration
{
// creating with type alias
type Demo = Value
// values foe enum
val day1 = Value("Sun")
val day2 = Value("Mon")
val day3 = Value("Tue")
val day4 = Value("Wed")
val day5 = Value("Thu")
val day6 = Value("Fri")
val day7 = Value("Sat")
}
Output:
Conclusion
In scala enum, we do not have any keyword named enum like other programming languages. Instead, we have an Enumeration class that we need to extend. Also, if we want to define enum values, we have to use the val keyword because to make it accessible. We can also assign some default values to the enum constant that would be the id for enum values.
Recommended Articles
This is a guide to Scala Enum. Here we discuss How Enum Function Work in Scala and Examples, along with the codes and outputs. You may also have a look at the following articles to learn more –