Initial thoughts on Scala

I just finished writing my first program in Scala, a simple Black-Scholes binomial-tree derivative pricer.

So far, the most notable observation I can make about the language is HOLY CRAP THAT’S A LOT OF SYNTACTIC SUGAR.

Got a method? You can call it using “object.method()”, like in C++/Java/C#. If it takes zero or one parameters, you can also call it as “object method” or “object method parameter“, so you can use it as an operator. (Left-associative, unless the method name ends with a colon, then it’s right-associative.)

Do you have a function that only takes one parameter? You can call it with curly braces instead of parentheses, which lets you use the loan pattern.

In a situation where you need to provide a function where you’ll only need to reference each parameter once, and in the order provided? Just use underscores in the function body for the parameters rather than having to declare them.

Calling a parameterless function? You can leave off the parentheses. Probably.

There’s a lot of stuff like this. The good thing about all this is that it lets Scala have syntax for features that resemble those in other languages, and a lot of that is in libraries, not the base language specification. Lists can be created and pattern-matched using the cons operator like Lisp. Case classes look a lot like Haskell, and are similarly suited for pattern-matching. You can create actors and send them messages using “!” like in Erlang. You can magically grab the most relevant thing in some contexts with “_” like in Perl.

Traits are a substantial improvement over single inheritance + interfaces (à la Java/C#) or multiple inheritance (à la C++).

For a language that’s all about strong typing, its inference system has some surprising weak spots. Type erasure prevents you from being able to match on a parameterization of a generic class, and I already got caught by a failure to infer types when dealing with zipped lists.

For all that kvetching, though, it’s got most of the nice features that I’d expect from both object-oriented and functional languages: first-class functions, immutable data types, concise syntax for map/reduction/folding, classes, lazy evaluation, singleton objects, generics, etc.

Leave a Reply