Scala

Scala Option

The Scala Option is defined as a container that can either hold the value “Some” or “None”. In the case of the former value, an actual value assigned to a variable will be captured, whereas in the latter case, the “None” value or any value of the programmer’s choice will be captured within the Option container. It is considered extremely useful when you do not want to waste the “Some” or “None” output rather;, you want to utilize it in conjunction with some other function of Scala for achieving the desired functionality. Therefore, in this guide, we will first enlighten you on the significance of using the Scala Option in Ubuntu 20.04. After that, we will explain to you three different examples that will use the Scala Option in Ubuntu 20.04.

Why should we Use the Scala Option in Ubuntu 20.04?

There are some functions in Scala that either produce “Some” output or “None” output. For example, the get() function of Scala either produces a result or returns “None” as the output if no relevant result is found. To depict this, we have designed the following Scala script for you:

In this script, we have created a map with the title “provinces” and assigned two different key-value pairs of provinces and the respective countries to which they belong. Then, we have used the “get()” function twice to see if the passed provinces belong to the declared map or not. If yes, then this function will return the respective country to which that province belongs; otherwise, this function will return “None”.

To see if there are any compilation errors in this script or not, we will first compile it with the command shown below:

$ scalac Option.Scala

After a successful compilation, we will execute this script with the following command:

$ scala Countries

The output produced as a result of running this script is shown in the image below:

You can see that the output produced from this script is exactly the same as we expected; however, this “Some” and “None” output seems to be of no use now, i.e., we cannot do any further processing with it for the time being. Nevertheless, by using the Scala Option, we can utilize this output for some useful purposes that will be discussed in the following section.

Using the Scala Option in Ubuntu 20.04:

The following three examples will use the Scala Option in different ways. First, we will use the Scala Option with the “get()” function. Then, in the second example, we will use it with the “isEmpty()” function, and finally, we will use it with the ” getOrElse()” function of the Scala programming language in our third example.

Example # 1: Finding out if a given Province belongs to the Provinces Map or not:

This script will be somewhat similar to the one we have shared above. However, we will be using the “get()” function with the Scala Option this time. The following script demonstrates this:

After creating a map of provinces in this script, we have used the “get()” function within a user-defined function “display”. We have created this function with two cases of the Scala option, i.e., Some and None. If a match is found, this function will return the respective country’s name; otherwise, it will return an “x” symbol.

When we compiled and executed this script, the result shown below was displayed, which depicted that “Punjab” belongs to our map whereas “Shandong” does not.

Example # 2: Using the Scala Option with the “isEmpty()” Function:

In this example, we will be using the Scala Option with the “isEmpty()” Boolean function that either returns “true” or “false” depending upon the situation. The following Scala script demonstrates this:

In this script, we have declared two values, “v1” and “v2”. We have assigned the “Some” value of the Scala Option to “v1” i.e., 27, and “None” value to “v2”. After that, we have simply called the “isEmpty()” function one by one with both the values to check whether the output is “true” or “false”.

The output shown in the image below depicts that the first return value is “false” and the second return value is “true”. It means that our value “v1” was non-empty and “v2” was empty, which is absolutely correct.

Example # 3: Using the Scala Option with the “getOrElse()” Function:

Finally, we will be using the Scala Option with the “getOrElse()” function. However, before proceeding with the example Scala script, we will introduce you briefly to the “getOrElse()” function of Scala. This function returns the value assigned to the variable if it exists; otherwise, it returns a default value that is passed to this function. You will gain more clarity regarding the usage of this function by going through the Scala script that follows:

We have kept the values “v1” and “v2” exactly the same as our second example. However, instead of using the “isEmpty()” function this time, we have used the “getOrElse()” function of Scala with the default value “22” i.e., if a value for a variable will not exist, then, “22” will be assigned to that particular variable.

Upon executing this script, we find out that the value that was assigned to “v1” was printed as it is, whereas since no value was assigned to “v2” in our script, that is why the value “22” was assigned to it in the output.

Conclusion:

By now, you would have clearly understood the concept of using the Scala Option in Ubuntu 20.04, which was also the main goal of this article. We first shared with you a Scala script that does not use the Option container to highlight its significance. Then, we discussed three examples with you one by one that used the Scala Option with different functions of Scala. So now, we are confident that you would have developed a sound understanding of the Option container of the Scala programming language.

About the author

Saeed Raza

Hello geeks! I am here to guide you about your tech-related issues. My expertise revolves around Linux, Databases & Programming. Additionally, I am practicing law in Pakistan. Cheers to all of you.