Intro to the Scala REPL

  • If you are familiar with python development then the Scala REPL is somewhat similar to the Python Shell, but for Scala.
  • This will be confusing at first, since scala is a compiled language and you might be wondering how can an interpreter exist.
  • Technically REPL stands for Read Eval Print Loop.
  • Whatever code that you type in is compiled and run on the fly.
  • First it reads the code, then evaluates it, then prints if there any output statements and then this is looped till we exit the shell.Hence the name REPL.

If you have installed the REPL correctly, then if you type scala in your console/terminal, then your screen should be something similar to below

Scala REPL

There are two details to make note of.

  • The Scala version
  • The JDK/JVM it is running on top of

Just to remind that scala runs on top of the JVM

Entering and exiting the REPL

As in the previous example, you can enter into the shell by typing in Scala and exit out of them by typing in :quit

Type in :help which brings up lot of help options.Over the time we can achieve mastery, until then :help can be our Jarvis Co-pilot

Ok, let's start with some simple examples.

Basic Calculator expressions

Note down two things, res0-4 and there is an Int besides it, which we will discuss in the following slides.

  • All the evaluation results are stored in variables named as res 0- till required.These are auto assigned names and live throughout the session of the shell in memory.Besides these we can also store our own.
  • Notice that we did not mention any types in our expression and it automatically got inferred as Integers(Int).This is something called type inference which we will be discussing later.
  • Final Thoughts

    Hopefully that should get you started with the REPL.We will continue to explore Scala through the REPL as well as use Intellij.

    The REPL is particularly handy to write some quick code to test out scala logic.The error messages are also quite helpful.Now with armed with this tool, let's start to explore the world of Scala.