Tuesday 17 November 2009

Foray into Scala

I've recently spent some time refreshing my Scala skills, perusing through the excellent book "Programming in Scala" by Oderski et al. Scala is a hybrid functional / object oriented programming language that can run on the Java Virtual Machine (and also the .NET platform) like Groovy, JRuby, JYthon, etc... An obvious benefit of running on the JVM is the potential for reuse of existing libraries.

While at HP Labs Bristol, I had spent a fair amount of time working with Groovy to build research prototypes, and I really liked its concise, easy to read syntax, its support for functions (including closures), the ability to alter the code at run time with meta-programming, processing XML streams, etc.. Although I really like Groovy, I've always had some nagging problems with it. Its dynamic nature is very powerful, but some errors that ought to be picked up by a compiler may only be discovered at run-time as the code gets executed. Second of all, the meta-object protocol which makes the dynamic aspect possible is an additional layer of indirection which makes the code run slower.

Scala manages to implement some of the best features I like in Groovy by being a statically typed language with a smart compiler with advanced type inference. This allows you to write very concise code, leaves little room for run-time errors and plays nicely with the IDE to support things like refactoring.

Let's illustrate the Scala syntax with an example which displays followers and following lists of a dummy Twitter account:

Few points to mention:
  • The Scala parser support semicolon inference, so in most cases ";" are optional.
  • object is the Scala notation for a singleton object.
  • Application is the shorthand for creating an executable object with a main method. The body of the main method is the code within the curly braces.
  • val is used to indicate final variables.
  • following is a strongly typed list of string.
  • However, the type information can be omitted. In the definition of follower, the compiler is smart enough to work it out by itself. This makes the code a lot leaner.
  • Scala supports functions to iterate through the content of the followers and following lists.
The Scala parser has built-in support for XML literals and the language provides libraries to read and write XML data structures. Let's extend the code to create an XML representation of the followers list.

I have not tried to write the same logic in Java, but I suspect it would be a lot longer. The Scala parser knows how to handle text within XML angled brackets and allows you to mix in valid Scala expressions within { }. The output is as follow:

This is pretty neat!!!

Scala does not have the notion of operators; things like +, -, * are actually methods on classes, and what's more, you can define your own implementations. Other symbols can be used for method names, such as ! or -->. The Scala parser is more advanced than the Groovy parser where only a handful of operators can be modified.

Here is a dummy example based on the Twitter metaphor where I use ! to simulate sending a message to a Twitter user.


Like Erlang, Scala implements the Actor model of concurrency which enables the implementation of concurrent programs using a message passing paradigm instead of shared memory access. As in Erlang, the ! symbol is used to exchange messages between actors, but in Scala, ! is actually a method on an Actor class implemented in an external library (i.e. not part of the core language syntax). The ability to extend the language in this way is very powerful.

There is a lot more about Scala that deserves to be written, to list a few:
  • Built-in control structures with function literals.
  • Functions and closures.
  • Traits (mix-in)
  • Package and import
  • Pattern matching
  • Actor model of concurrency
  • Lift web framework enabling easy real time web applications.

I shall write more in the future illustrating some other aspects of the language.

No comments:

Post a Comment