Updated April 20, 2023
Introduction to Scala REPL
Scalarepl provides us a way to evaluate expressions in scala. Repl in scala we can say a command line interpreter. If you want to see it we have to run scala command on your command shell. This repl stands for Read-Evaluate-Print-Loop in scala. We provide some input on the command line interface and these stats reading those input from interface. After reading them carefully it start to evaluate those expressions based on our input then it prints the output on the screen like and this process keeps on going and repl is again ready to read our input and so on and goes into the loop.
Syntax:
$scala
This is the command that we have to run while working with repl in scala. Just open your command line interface and run this command there. You will some lines of code automatically gets printed.
How does REPL Command Work in Scala?
As we know that repl is an interactive interpreter shell. This is used to read the expression or we can say to evaluate the expression in scala.
Let’s have a look of its working in step see below:
- Read: When we type any input on the shell so it immediately start reading those inputs. So the first step it performs is reading the input from the command line shell.
- Evaluate: The second operation that it performed Evaluate. Immediately after reading the input, it perform its the next step that is to evaluate them correctly.
- Print: The third operation is to print the output. After reading and evaluating the expression from the command line interpreter it just print the output on the shell. It is the result of our expression.
- Loop: When all this process get performed successfully it again start performing the same steps. That means it is again ready to take the inputs and read them followed by evaluating the expression and print the output on the shell and this process goes on the loop.
When the repl evaluating an expression so at that time it bind those expression into the executable template. After this step it compiles the executable template and prints the result or we can say the output on the screen. We have so many command to deal with repl in scala or these are the features provided by them to work with expression in scala.
Lets’ discuss each command in detail:
- power: This commands will import all the compiler components. Also, this command is used to enter into the power mode.
- paste -raw: This command is used to disable some code which is defined in a package.
- help: this command will display the list of all the commands that are present and we can use.
- load: This command is used to load file and this fie will contain the REPL input.
- settings: With the use of this command we can modify the settings of our compiler.
- replay: This command is used to give replay to session with some modified settings.
- javap: This command is used to for the inspection of the class artifacts.
- paste: This command helps us to enter the object and class.
- -Yrepl-outdir: This command helps us in the inspection of our artifacts with this help of some external tool.
When we type any line on the command line interpreter shell that is these lines would be compiled separately not all at once. Suppose we have some dependency of the previous lines on the command line then this dependency would be automatically be imported by the help of generated imports. The code that we type in the shell will be bind as an object or a class.
Let’s see one simple example to for beginners to start with; openyour command line and try to type the below line of code there;
val a = 1
val b = a + 1
We are assigning two variable here a and b. But b has dependency on the variable a here. These lines of code will be compiled separately as we typed them on the shell. Also, the dependent code will be automatically imported. After these two lines of code, we will have the output as 2 here. Just freely open the command line type your code there.
Examples of Scala REPL
First try to type scala on the shell the press enter to check everything is working fine and you will see some lines would get printed there. Like this format see below;
Example #1
In this example we are printing out the sum of variables using repl in scala.
Code:
var a =10
a: Int = 10
var b =20
b: Int = 20
var c = a+ b
Output:
Example #2
In this example we are trying to concatenate two strings in scala using relp tool.
Code:
var a = "hello to all !"
a: String = hello to all !
var b = "this is example for repl in scala !!!"
b: String = this is example for repl in scala !!!
var c = a + b
Output:
Example #3
In this example we are using list from collection data structure and printing the length, max and min value-form list by using min, max, and size method available in scala.
Code:
var list = List(900, 300, 400, 500, 10 , 40 , 100)
list: List[Int] = List(900, 300, 400, 500, 10, 40, 100)
var result = list.size
result: Int = 7
var max = list.max
max: Int = 900
var miv = list.min
miv: Int = 10
Output:
Conclusion
This is a powerful tool to evaluate the expression in scala with so many features like switch between class and object and many more. It exactly works in a way like it stands for.(read, evaluate, print and loop). It is also easy to use because we just start our work on the command line interfaces by using the scala command there. So it is very handy to use for programmers to calculate the complex expression in scala.
Recommended Articles
We hope that this EDUCBA information on “Scala REPL” was beneficial to you. You can view EDUCBA’s recommended articles for more information.