How to use Kotlin Enum in Ubuntu 20.04?
For using the Kotlin enumerations in Ubuntu 20.04, you can see the examples shown below:
Example # 1: Printing the Enumeration Constants in the Form of a String in Kotlin:
In this illustration, we will demonstrate to you the method of creating an enumeration in Kotlin and printing its constants in the form of a string. You can do this by using the Kotlin script shown in the following image:
To create an enumeration in Kotlin, you need to use the “enum” keyword followed by the “class” keyword. Then, you can have any name of your choice for the enumeration. We have named our enumeration “Colors.” In this example, we want all the constants of our enumeration to have the “Integer” type values. Then, within our enumeration, we have defined three named constants, i.e., Red, Blue, and Green. We have also assigned integer values to these named constants, which are the RGB codes for these colors. The different named constants of an Enumeration in Kotlin are separated by commas. Then, we have defined our “main()” function in which we have used the “joinToString” function within the “println” statement for printing all the named constants of our enumeration in the form of a string.
Then, we have compiled our Kotlin script with the command stated below:
After that, we have executed our Kotlin script with the following command:
All the named constants of our enumeration in the form of a string are shown in the image cited below:
Example # 2: Printing the Enumeration Constants in the Form of a List in Kotlin:
In the example discussed above, we have printed all the named constants of a Kotlin enumeration in the form of a string. In this example, we will show you the process of printing all the named constants of a Kotlin enumeration in the form of a list. You can use the following script to achieve this goal:
The enumeration used in this example is the same as we declared in our first example. Though, this time, inside our “main()” function, we have used the “toList()” function within the “println” statement for displaying all the named constants of our enumeration in the form of a list.
All the named constants of our Kotlin enumeration in the form of a list are shown in the image beneath:
Example # 3: Printing the Index of a Kotlin Enumeration Constant on the Terminal:
The indexing of the named constants within a Kotlin enumeration works exactly like array indexing in any other programming language, i.e., the indexes always start with “0”. If you want to know the position or indexing of any specific named constant of a Kotlin enumeration, then you can use the following script:
We have used the same Kotlin enumeration in this example as well that we had created for our first example. Then, inside our “main()” function, we have defined a value “color” and have assigned to it the value “Colors.Red” i.e., it will point to the “Red” named constant of our enumeration. After that, we have called the “ordinal” function with our created value for printing the index of the specified named constant of our enumeration.
The index of our specified named constant is shown in the image beneath:
Example # 4: Printing the Values of the Enumeration Constants in Kotlin:
In this example, we will teach you the method of accessing the values of all the named constants of a Kotlin enumeration. For doing that, you can take a look at the subsequent Kotlin script:
Again, we have utilized the similar Kotlin enumeration that we have used in the instances above. Then, inside our “main()” function, we have used a “for” loop that iterates through all the named constants of our enumeration. Inside this loop, we have used a “println” statement for printing the associated values with these named constants.
The values associated with all of our named constants (after getting converted to integer) are shown in the cited below image:
Example # 5: Using the Kotlin Enumeration with the Kotlin When Expression:
In this example, we will try to pair up the “when” expression of the Kotlin programming language with the enumerations for printing a customized message on the terminal. For doing that, we have used the following Kotlin script:
In this script, we only wanted to use the named constants of an enumeration; therefore, we have simply removed their associated values. Then, within our “main()” function, we have defined a variable “color” and have assigned to it the value “Colors.Blue” i.e., it will point to the named constant “Blue.” Then, we have used the “when” expression over the “color” variable, and inside this expression, we have three different conditions for checking the value of the “color” variable for printing customized messages on the terminal.
The result of this Kotlin script is shown in the image beneath:
Conclusion:
Today, we wanted to familiarize you with the concept of the enumerations in Kotlin in Ubuntu 20.04. We first described the purpose of the Kotlin enumerations briefly. After that, we discussed five different examples with you that implemented the Kotlin enumerations in different ways, and we also talked about a few associated functions with enumerations. By going through all of these examples, you will get an excellent idea regarding using the enumerations in Kotlin Ubuntu 20.04.