First Thoughts on Scala
Over the past week or so I’ve been looking into the Scala programming language. If you aren’t familiar with it, Scala is one of a group of new-ish languages including Groovy, Clojure, and Nice (as well as new implementations of preexisting languages like Jython, JRuby, and Rhino) that run on the JVM (either interpreted or compiled to Java bytecode). Over perhaps the past decade the JVM has been increasingly seen as an attractive target platform for language development for several reasons:
- Implementation in Java instead of C
- Features like garbage collection, portability, and a huge standard library come for free
- Languages benefit from advances and optimizations in the JVM
(This is part of a trend pointed out as Prediction #4 in a list of 10 predictions about software Steve Yegge made about 5 years ago.) In fact, targeting a virtual machine has almost become the only way to implement new, fancy languages with reasonable performance, portability, and implementation time.
Scala has an interesting mix of features. Some are taken from functional programming such as higher-order and anonymous functions, closures, partial function applications, and algebraic data types. It has pure OO (everything is an object, even functions) with mixins to compose functionality. Probably the most distinguishing feature of Scala is that it is statically typed (with some pretty nice type inference). For all this, it has a fairly small syntax (significantly smaller than Java, for instance).
As implied by the name, Scala tries to be as scalable as possible. Here are a couple of ways this is done (and I’m sure there are many others that I haven’t seen). Scala programs can be interpreted like scripts: if hello.scala contains
println("OHAI")
then this can be run by
$ scala hello.scala OHAI
They can also be compiled to class files. If hello.scala instead contains
object HelloApp { def main(arg: Array[String]) { println("OHAI") } }
then we can do
$ scalac hello.scala $ scala HelloApp OHAI
What really makes Scala scalable, though, is how its small but powerful syntax allows creation of new constructs that almost look like new syntax itself. In a great talk by Martin Odersky, the creator of Scala, he presents three nice examples of features that can be concisely added to Scala due to its extensibility: the using keyword, break and continue, and Erlang-style actors (the latter two are included in the Scala library).
I’m just getting started with Scala, but I’ve been very happy with its thorough documentation and the large collection of documents available to help learn the language. For instance, I have found Martin Odersky’s Scala By Example to be a great introduction. It’s nice to see a language that makes it so easy for beginners to figure out what’s going on (I’m looking at you, Cobra). I’m also having a good time reading “Programming in Scala”, the comprehensive and extremely well-written language guide. Not only does it explain Scala very well, it is easy to read and includes tidbits such as this (vars are mutable variables; vals are immutable):
If you’re coming from an imperative background, such as Java, C++, or C#, you may think of
varas a regular variable andvalas a special kind of variable. On the other hand, if you’re coming from a functional background, such as Haskell, OCaml, or Erlang, you might think ofvalas a regular variable andvaras akin to blasphemy.
Scala has some traction in the software community. The most well-known example, of course, is Twitter; last year they switched their message queuing from Rails to Scala, in their continuing quest to turn millions of dollars of venture capital into the hippest code possible. (Also there was a bit of a case of their backend exploding when things started heating up; it seems a bit telling that they switched to from Ruby to a language that has the first two syllables of ’scalable’ in its name.) There is also a Rails-like web framework for Scala, Lift.
Given my great initial impression, I can only hope that Scala becomes more widespread. I might post more thoughts on Scala quirks and features (as well as my improved vim indent file) later on.
No comments yet.