Using Scala REPL:
For using the Scala command-line interpreter shell, i.e. Scala REPL in Ubuntu 20.04, you can read through the following sections of this article:
How to Enter the Scala Command Line Interpreter Shell?
For entering the Scala command-line interpreter shell, you will have to launch the terminal of your Ubuntu 20.04 system and then execute the command mentioned below:
When you execute this command, you will instantly get to see the Scala command-line interpreter shell, i.e. REPL, within your terminal, as shown in the following image:
How to Access the Help Manual of Scala REPL?
Before proceeding with the usage of Scala REPL, you might want to know how to use it. For that, you need to access its help manual. While staying within Scala REPL, you need to run the command shown below:
The help manual of Scala REPL is shown in the following image. With the help of this, you can execute all the commands associated with Scala REPL.
How to Add Two Numbers in Scala REPL?
Now, we will explain how you can add two numbers in Scala REPL. First, you need to declare and initialize two variables in the manner shown below:
You can assign any values of your choice to these variables. Similarly, you can have any name of your choice for these variables. Upon a successful declaration and initialization, the following output will appear on the shell:
Then, you can add these two variables with the help of the command shown below:
The output after adding these two variables is shown in the following image:
How to Get More Information about the Temporary Variable in Scala REPL?
If you want to know more about the temporary variable created recently in Scala REPL, then you will have to execute the command given below:
You can replace “0” with any number that appears with your recently created temporary variable.
The main information about our specific temporary variable is shown in the following image. It shows that our temporary variable is of integer data type.
How to Add the Elements of an Array in Scala REPL?
For adding the elements of an array in Scala REPL, you first need to declare and initialize an array in the manner shown below:
If there will be no errors in the creation and initialization of the array, then you will get to see the following output on your shell:
Now, to calculate the sum of the elements of this array, you will have to invoke the “sum” method with the name of this array in the manner shown below:
The sum of the elements of our array in Scala REPL is shown in the following image:
How to Print a Hello World Message in Scala REPL?
You can also print a Hello World message in Scala REPL by running the command given below:
The Hello World message will be printed on the shell with the declaration of this string as shown in the following image:
How to Replay all the Previous Commands in Scala REPL?
If you want to replay all the commands that you have previously executed within Scala REPL, then you will have to use the command shown below:
When the above-mentioned command runs, all the previously executed commands from the current Scala REPL session will be executed one by one or in other words, all of these commands will be replayed as shown in the following image:
How to Check the History of Scala REPL?
You might also want to check the history of all the previously executed commands within Scala REPL. For that, you will have to run the command shown below:
The following image shows the complete history of the commands that have been executed within Scala REPL:
How to Exit from the Scala Command Line Interpreter Shell?
After doing everything within the Scala command-line interpreter shell, you can exit from Scala REPL by running the command shown below:
When you execute this command, you will instantly exit from the Scala command-line interpreter shell. You will get back to the normal terminal interface as shown in the following image:
Conclusion:
This article was intended to talk about Scala REPL in Ubuntu 20.04. Since this command-line interpreter shell comes by default with the Scala programming language, we did not discuss anything about its installation in Ubuntu 20.04; rather, we began with a brief introduction of Scala REPL and the purpose of its existence. Then, we taught you how you can get started with its usage by entering the Scala command-line interpreter shell. After that, we progressively taught you how to perform various tasks using Scala REPL. Moreover, we also shared how you can exit from Scala REPL and return back to the regular Ubuntu terminal. Now, you can also experiment with different programs on your own while using Scala REPL in Ubuntu 20.04.