☍ Scala in a nutshell-Introduction
Scala is a JVM languages designed by Martin Odersky in early 2001 and it brings features of object-oriented programming and functional programming together. Scala is competing again several new JVM languages including but not limited to Groovy, Clojure, JRuby and Jython. Scala resembles Python (and Ruby) in many ways and as a Pythonist I found it real easy to switch on Scala. The purpose of this tutorial series is to give Python geeks an idea about the similarities and differences between Scala and Python world.
Before we start this tutorial, below are some suggested reading for both Scala as well Python. I will be pulling lot of ideas and examples from these sources.
- Programming in Scala1
- Scala for the Impatient (You can download a free PDF version here. A set of exercises at end of each chapter)
- Beginning Scala (This book is a useful complement to Programming in Scala)
- Programming Python (First class reference text on Python)
- Python Pocket Reference (A Swiss Army knife for Python developers)
Current stable version of Scala is 2.9.1. You can download latest stable release and installers for Scala here. One of the key feature of Scala is Actors. In Scala Actors provide a concurrency model which is lot a easier than Java’s native concurrency model. To add this creators of Scala has developed Akka a framework which simplifies writing concurrent, scalable and highly available applications through Actors. To get the most out of Scala’s scalability features, use of Akka is highly desirable. Although Akka can be installed separately from Scala, it is better to use a simple, pre-integrated stack together with Akka provided by Typesafe.
After installation make sure that the scala/bin directory is on the PATH ( User environment are set up correctly with
SCALA_HOME and bin
Scala Interactive Interpreter (REPL)
Scala Interpreter (often called a REPL for Read-Evaluate-Print Loop) is Python equivalent of Interactive Mode. To get started type
scala in your terminal.
Initially you will find that Scala REPL takes bit longer load time than expected due to enormous start-up cost for the REPL. According to this Stackoverflow thread using REPL gradually causes JVM bytecode to be converted to native code, after which it’s very fast.
Now let’s check the
:help on REPL
You can use Scala REPL as a calculator pretty much like Python interactive mode,
The key difference is
res or automatically generated
result variable names such as
res1 when a variable name is not provided. REPL interpreter also displays the type of the result in this case
Int via Scala’s built-in type inference mechanism. In Scala, often it is not necessary to specify the type of a variable, in fact the compiler can infer the type from the initialization expression of the variable.
For the record the Scala is statically typed language with type inference and it is completely orthogonal to dynamic languages like Python or Ruby. Although type inference may confuse some of us from Python or Ruby world which is actually done by the compiler (see more details on static vs dynamic).
Scala REPL Tab Completion
Scala REPL provides tab completion or suggestion which can be used for package and class completion as well as member completion (suggesting static and instance methods for a Scala object). For member completion use
object. and tab.
Python does not provide an out of the box tab completion solution like Scala but you can use either
rlcompleter module or use iPython
Scala REPL Power Mode
Scala REPL power mode can be invoked by
First of all, you can see above that power mode adds new variables (val type), command options and defintions. For instance if you type
:help before and after power mode activation you can see following additional cammand
:wrap is more like a Python decorator. You can define a custom method to wrap around each REPL line. For instance, let say you want to profile each line on REPL by calculating it’s run time. Define a custom method which takes the code body and execute it and return the run-time (credits).
Scala Script Mode vs Compile Mode
Rather than typing on REPL you can also write your Scala code on a file, either in script mode (Scala Script) or compile mode (Scala Program). In script mode Scala files do not have an explicit main method. In both modes Scala files have an extension
.scala. You can also run a Scala file as a shell or bat script if appropriate header is added, for instance in Unix
and for Windows
%0 is set to the name of the file,
%* is set to the positional parameters.
Loading & Running Scala Script in REPL
Loading a Scala script in REPL is quite straight foreword. Create a Scala script file
test.scala and add some code to it
:load script.scala arg1 arg2
Again it is quite similar to
execfile("script.py arg1 arg2") in Python interactive shell.
Alternatively, you can open the Python interpreter by typing
python -i script.py on your terminal. It will execute
script.py and then drop you into interactive mode with the environment left behind by
script.py. Moreover you can also add
#!/usr/bin/python -i directly into start of your Python scripts.
Running Scala Script on Terminal
You can run you Scala script as
scala script.scala arg1 arg2
Note command-line arguments can be accessed in your script with the
argv variable which is an Array[String].
In Python you can access the command-line arguments using
sys.argv which also includes name of Python script file as first argument.
Compile and Execute Scala Program
scalac command to compiles one (or more) Scala program file(s) to generates Java bytecode which can be executed on any standard JVM. To compile using
scalac source files must contain one or more class, trait, or object definitions.
Note that complilation using
scalac is very slow due to overhead related to enormous start-up cost every time you run compiler. To speed up this, standard Scala distribution ships with a compiler daemon called
fast Scala compiler) which you can run like,
When you comiple with
fsc first time it will take longer due to delay in starting the daemon but after that the compliation will be quite fast as daemon will already be running in background.
To compile and execute a Scala program, you must supply the name of a standalone singleton object with a main method that takes one parameter, an
Array[String]. Singleton object with a main method acts as the entry point.
Obvisouly you can simply run it as a script,
or better compile and execute it,
HelloWorld.scala will generate
HelloWorld.class file which can be executed using
scala command. Note that bytecode file
HelloWorld.class corresponds to object defintion of same name. As best practise you should always use same source file name as object name.
Corresponding shell script version
HelloWorld.sh for Unix which you can run from terminal as
./HelloWorld.sh Abhishek if script has executable permission.
Written by none other than Martin Odersky - the creator of Scala ↩
blog comments powered by Disqus