Alex headshot

AlBlue’s Blog

Macs, Modularity and More

Introduction to Scala – the interpreter

Howto 2007 Scala

Scala is a strongly typed hybrid functional/object-oriented language that can be both interpreted and also compiled into byte-code and run on top of a Java Virtual Machine. (Scala can also be compiled into MSIL to run on top of the .Net framework; but I'm only going to focus on the JVM integration in these posts.) The language is loosely based on Java but is much more extensible owing to the fact that functions are first-order values and higher-ordered typing allows programs to be statically type-checked and optimised by the JIT at run-time. In contrast, other dynamic languages such as JRuby and JPython tend to be executed with much more indirection, and as a result, the JIT isn't able to perform as many types of optimisation.

So what does the Scala language look like? Well, the easiest way to get started with a new language is to give it a go. You can download Scala from www.scala-lang.org; once downloaded and unzipped, the scala interpreter is in the bin/ directory, and when you start up will display a message like this:

Welcome to Scala version 2.6.0-final.
Type in expressions to have them evaluated.
Type :help for more information.

scala>

Result! You're now running Scala. OK, so it's not much to look at, but if you've got a burning desire to do some mental arithmetic puzzles, you can type them in at the shell (hit enter to see the result):

scala> 3+4
res0: Int = 7

Oh, the excitement. Yes, given any two numbers (pick two numbers, any numbers) you can calculate the sum. And so, the journey into Scala begins.

So, what's up with the “res0: Int = 7”? As well as telling you the result (7), Scala's also telling you the inferred type (Int) and since the number might be lucky and come in use later, it's putting it into a variable called res0 for you. If you come from a Java or C background, you'd be more accustomed to seeing Int res0 = 7. You can read the : as 'is of type', so what the Scala interpreter is telling you is that the result of the calculation has been put in, a variable* called res0 of type Int.

You can then use this value in later calculations. Should you desire to know what the result of adding this to 5, for example, you can find out:

scala> res0 + 5
res1: Int = 12

Great; handy to deal with all those arithmetic queries you wanted to find out about but were afraid to ask. What about division?

scala> 3/4
res2: Int = 0

Oops. It's not that Scala can't be used for division; but rather that (following the same rules as Java) both 3 and 4 are treated as Int, and thus it's doing integer division. If we want to do floating-point arithmetic, we can tell Scala to treat them as such by adding a decimal point:

scala> 3.0/4
res3: Double = 0.75

The observant amongst you will no doubt have drawn a correlation between these and other types in Java, and are wondering if they're similar. The answer is; yes, they are; Int is a signed 32-bit type, Double is a signed 64-bit IEEE754 floating point value, with all of the same limitations that you know about:

scala> 1<<31
res4: Int = -2147483648

scala> res4 - 1
res5: Int = 2147483647

The second thing to know about Scala is that there are both values and variables (or just vals and vars). Values are similar to const in other languages; once created, the value of them can't be changed. The res* are all vals; if we try to reassign it, we will get an error:

scala> res5 = 5
<console>:5: error: assignment to non-variable
  val res6 = {res5=5;res5}

Given that res5 is a value, rather than a variable, we can't change its definition. Here, the interpreter is saying that it was trying to create a new val called res6, and it was the result of doing res5 = 5 followed by returning the value of res5. In fact, because it was an error, res6 doesn't exist, and we can't therefore access its value:

scala> res6
<console>:4: error: not found: value res6
  val res7 = res6

So, what if we want to create our own? Well, the error message gives us a hint at the syntax:

scala> val c = 299792458
c: Int = 299792458

We wouldn't want the speed of light changing, so any reference to c will give us the same value. Whilst it's possible to write programs without any changing variables (XSLT is a good example), Scala also gives us the ability to create variables too:

scala> var total = 0
total: Int = 0

We can now manipulate this to our heart's content:

scala> total = total + res1
total: Int = 12
scala> total = total + c
total: Int = 299792470

What about operations like +=? Yes, we can use these to update the variable as well:

scala> total += 15
res10: Unit()

What's this? Well, += is a kind of statement rather than expression, and (roughly speaking) Unit is a kind of void type. The only literal Unit is ():

scala> ()
res11: Unit()

We've covered the basics of the Scala interpreter. To finish, we just need to quit the interpreter. You can either use the interrupt (Ctr+C) or EOF (Ctrl+D) on Unix systems, or EOF (Ctrl+Z) on Windows platforms; alternatively, use :quit.

scala> :quit

We'll talk a little more about what we can do in the interpreter in the next (un)exciting post. Stay tuned.