Scala REPL is an interactive command line interpreter shell, where REPL stands for Read-Evaluate-Print-Loop. It works like 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 screen and then it is again ready to Read and this thing goes in 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.
- Either an object Or a class can be wrapped by user code the switch used is -Yrepl-class-based.
- Each and every line of input is compiled separately.
- The Dependencies on previous lines are included by automatically generated imports.
- The implicit import of scala.Predef can be controlled by inputting an explicit import.
We can start Scala REPL by typing scala command in console/terminal.
Let’s understand how we can add two variable using Scala REPL.
In first line we initialized two variable 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 expression of sum with defined two variable. with this Scala REPL printed sum of expression on 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 variable as same like we created it.
We can get more information of these temporary variable by calling getClass function over these variable 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 suggestion that we can apply on variable by pressing TAB key.
- IMain of REPL is bound to $intp.
- The tab key is used for completion.
- lastException binds REPL’s last exception.
- :load is used to load a REPL input file.
- :javap is used to inspect class artifacts.
- -Yrepl-outdir is used to inspect class artifacts with external tools.
- :power imports compiler components after entering compiler mode.
- :help is used to get a list of commands to help the user.
- Julia - REPL
- Scala Tutorial – Learn Scala with Step By Step Guide
- Scala short <(x: Short): Boolean
- Scala short <(x: Char): Boolean
- Scala Extractors
- Scala | Partially Applied functions
- Scala String indexOf(String str) method with example
- Scala String contentEquals() method with example
- Scala Keywords
- Scala Int /(x: Int) method with example
- Scala Int /(x: Short) method with example
- Program to print Java Set of characters in Scala
- Scala Map size() method with example
- Scala SortedMap addString() method with a start, a separator and an end with example
- Scala Iterator addString() method with example
- Scala String substring(int beginIndex, int endIndex) method with example
- Scala | Functions Call-by-Name
- Program to convert Java list to an iterator in Scala
- Scala Set &() method with example
- Scala | Type Inference
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.