Using the Enumerations in the Scala Programming Language:
To use the enumerations in the Scala programming language in Ubuntu 20.04, you can take help from the following four examples:
Example # 1: Printing the Values of a Scala Enumeration on the Terminal
This example will demonstrate the method of printing the values of a Scala enumeration on the terminal. The following Scala script will depict this method:
In this program, we have declared a class named “Enum” and have extended it from the Scala “Enumeration” class by using the “extends” keyword. Then, we wanted to create an object for holding the values of the enumeration class. We have declared this object using the class name with the “type” keyword and named it as “Value”. Now, this object will be used to assigned values to our enumeration. We have created three different variables of “val” type named “one”, “two”, and “three” and have assigned to it the names of different defence forces. Then, within our “main()” function, we have printed all the values of our enumeration on the terminal by using the “${Enum.values}” notation.
In order to compile this script, we have used the command shown below:
Then, to execute this script, we have used the following command:
The values of our enumeration are shown in the image below:
Example # 2: Accessing a Particular Value of a Scala Enumeration
Sometimes, you might only want to access a single value of a Scala enumeration. That value can easily be accessed by using the name that you have assigned to this value. For doing this, you can look at the following Scala script:
In this Scala program, we have continued the script that we have used above till the declaration and initialization of our enumeration. Then, we wanted to access only the second value of our enumeration. Therefore, inside the “println” statement, we have used the “${Enum.two}” notation where “two” represents the name of the second value of our enumeration.
Then, to see if we managed to access the correct value of our enumeration or not, we executed our Scala script. The image shown below implies that the second value of our enumeration has been printed correctly on the terminal. Therefore, in the same manner, we can use the name of any value of our enumeration for accessing that particular value.
Example # 3: Printing the ID of a Particular Value of a Scala Enumeration
At times, you might just want to print the ID associated with any particular value of a Scala enumeration. Whenever you create an enumeration in Scala and initialize it with some values, the IDs are assigned to these values by default in sequential order starting from “0”. It means that the first element of the enumeration will have the ID “0”, the second element will have the ID “1” and so on. This will remain like that until you manually change the IDs associated with the elements of your enumeration. You can see the following Scala script if you want to print the ID associated with any value of a Scala enumeration:
In this example, we just wanted to print the ID associated with the first value of our enumeration. For that, we have used the same enumeration that we had declared and used in our first two examples. Then, within our “main()” function, we have used the “${Enum.one.id}” notation for printing the ID of the first value of our enumeration on the terminal.
The ID of the first value of our enumeration i.e. 0 is shown in the image below:
Example # 4: Using the Scala Enumeration with Loops and Conditional Statements
Now, we will take the usage of the Scala enumerations a step further i.e. we will now be using the Scala enumerations with the loops and conditional statements. The Scala script for serving this purpose is shown in the following image:
Again, in this example, we have used the same enumeration that we have used in the examples above. However, this time within our “main()” function, we have first used the “foreach” loop that will iterate over all the values of our enumeration. Within this “foreach” loop, we have a “case” statement that is applied on a variable “x”. Then, we have an “if” statement inside which we wanted to compare the “x” variable with a value of our enumeration i.e. “one” in this case. As soon as this value will be encountered while iterating through the loop, a message will be printed on the terminal. However, if this value will not be encountered till the last iteration of our loop, then, the “case _” will be executed.
The image shown below displays the output of this Scala script:
Conclusion
We wanted to teach you the usage of the enumerations in the Scala programming language in Ubuntu 20.04 with the help of this article. For that, we first discussed the basic concept of enumerations in Scala and we also talked a little about the built-in Enumeration class of this programming language. Then, we furnished four different examples that helped in explaining the process of creating, populating, and printing the enumerations in Scala. Moreover, these examples also shared the process of accessing the values of an enumeration by their names and then accessing their default IDs by their names. Additionally, we also shared how these enumerations can be used with loops and conditional statements in Scala. You can go through these examples for getting started with the Scala enumerations.