Open In App

Scala | REPL

Scala REPL is an interactive command line interpreter shell, where REPL stands for Read-Evaluate-Print-Loop. It works as it stands for only. It first Read expression provided as input on Scala command line and then it Evaluate given expression and Print expression’s outcome on the screen then it is again ready to Read and this thing goes in a loop. In the scope of the current expression, as required, previous results are automatically imported. The REPL reads expressions at the prompt In interactive mode, then wraps them into an executable template, and after that compiles and executes the result.

Implementation Of REPL



We can start Scala REPL by typing scala command in console/terminal.

$scala

Let’s understand how we can add two variables using Scala REPL.  



In the first line, we initialized two variables in Scala REPL. Then Scala REPL printed these. In this we can see that internally it create two variable of type Int with value. Then we executed the expression of sum with defined two variables. with this Scala REPL printed the sum of the expression on the screen again. Here it did not have any variable so it showed it with its temporary variable only with prefix res. We can use these variables the same as we created them. We can get more information on these temporary variables by calling getClass function over these variables like below. We can do lots of experiments like this with scala REPL on run time which would have been time-consuming if we were using some IDE. With scala2.0 we can also list down all function suggestions that we can apply to variables by pressing the TAB key. Some More Important Features of REPL

Article Tags :