Friday, October 16, 2009

Scala vs. Scala

One bit of fallout from the JVM Language Summit I attended last month was a resolve to learn Scala.

I've been trying to write an implementation of a game my cousin designed, and I decided to use Scala, because the scope of the project seemed small enough that I could combine it with learning a new language, but large enough that it would exercise a fair amount of that language.

And so far, I'm pretty fond of Scala. The underlying semantics are close to Java's, because the language was conceived to run on the Java Virtual Machine, but the Scala compiler does its utmost to infer anything (like the type of an expression, or whether an identifier is the name of a variable or zero-argument method) that would be obvious to a human reader. The result is a great deal less boilerplate than in Java. Furthermore, Scala traits have fewer arbitrary restrictions on their content than Java interfaces, which makes it easier to compose your code the way you think it, instead of the way the compiler wants it.

In the dispute between functional vs. imperative languages, Scala comes down squarely on both sides. Scala seems to have two ways to do almost everything; consider, for example, the imperative-looking for (obj <- mySet; if isGood(obj)) yield transform(obj) vs. its functional equivalent, mySet.filter(o => isGood(o)).map(o => transform(o)). Which is better? I'm not sure I have enough experience with Scala to give a definitive answer, but I do find myself using both imperative and functional forms in my code, and I think the imperative form is easier to follow when you're doing something complicated, and the functional form easier to read when you're doing something simple. Because the rules for transforming between the two syntaxes are pretty straightforward, having both options in the language doesn't seem to impose as much of a mental burden as I had expected.

By making it clear that the dichotomy between functional and imperative languages is a false one, Scala does us the favor of freeing up our energies to argue about something else. On to the next flame war!