? ?

Why Scala Is Interesting

As anyone who reads this blog knows, my favorite programming language is Haskell. I love functional programming, and Haskell is a great vehicle for pushing functional programming to its limits. However, I try to keep up with other up-and-coming languages, especially functional ones, so of course I've been aware of Scala ever since it came out. At the time, I took a cursory look at it and didn't like what I saw; there was too much syntactic sugar for my taste and the language seemed excessively complex. However, some people I respect like the language, so I've been giving it another look. My comments are not based on a comprehensive knowledge of the language, just what I've gleaned from a couple of weeks of reading and playing around. Nevertheless, I already know enough to state that Scala is an interesting language and worth learning, particularly if you're coming from a Haskell background. I've been using the "staircase book" (Programming In Scala by Odersky et al), which BTW is a very well-written book which I recommend wholeheartedly.

There are two reasons I might want to learn a new programming language. One is that the language is useful for practical programming in a way that my current favorite languages aren't, and the other is that the language has some interesting features that I haven't seen before (or at least, haven't seen implemented as well). Scala appears to fulfill both criteria.

From a practical standpoint, Scala runs on the JVM, it's fast, and using Scala I can do GUI programming, web programming (using the Lift framework), and program mobile devices (using Android, though I gather that it's not yet a trivial exercise to get Scala code running on Android). Scala appears to be far ahead of Haskell in all of these areas. I expect that Haskell will catch up eventually, but I'd like to write these kind of applications now.

From a theoretical standpoint, the appeal of Scala is the way that it combines functional and object-oriented programming in a statically-typed setting. This is not easy to do. It's easy to do in a dynamically-typed language (for instance, Lisp with CLOS), but static typing brings in a lot of complexity, notably with respect to subtyping and inheritance. Haskell can support some aspects of OOP (notably, using type classes and existential types), but I haven't seen OO-style inheritance modeled in Haskell, even though it's often quite useful. Scala has a very sophisticated OO system, with features like explicit sub- and supertype annotations, covariance and contravariance annotations, etc. which goes beyond what I've seen in other OO languages. In fact, Scala implements all of its functional features as objects, so calling it an object-functional language is less accurate than calling it an extremely powerful OO language.

I think of Scala as kind of an object-oriented dual to Haskell; it's a best-of-breed language for its paradigm. (I'm ignoring dependently-typed functional languages such as Agda/Coq/Epigram here; I know they exist but IMO they can't yet be considered general purpose programming languages.) Whether objects are a more fundamental construct than functions in a statically-typed setting is unclear to me. In dynamically-typed languages, you can get objects from functions (assuming functions are closures), or you can get functions from objects, so the two notions are interconvertible. Whether or not this is the case with static typing, I think it's interesting to have two languages approaching this question from two different directions. On the other hand, I learned about OO from Smalltalk, and I have to say that I've never seen a statically-typed OO language that had the elegance of the simple message-passing model of Smalltalk. On the other other hand, I would much rather write a large program in Scala than in Smalltalk. (Also, Scala has an Actors library for when you really want straight message-passing.)

Another thing I like about Scala, and one it shares with Haskell, is that both languages have their roots in academia. When you're designing a language with a sophisticated type system, it really helps a lot if you actually know something about type theory. Both the Scala and Haskell designers clearly do, and this accounts for many of the good qualities of these languages. I hope that we're soon approaching the day when the kind of people who dismiss languages because of their academic roots in favor of real he-men "practical" languages will have to STFU because the evidence will show that languages designed by academics (i.e. people who actually know what they are doing) are simply superior at solving real-world problems.

I'm still getting my feet wet with Scala, and I look forward to learning more. But I still love Haskell and I'm not going to give it up either ;-)



Scala on Android

Actually, it is pretty easy to get Scala going on Android. There's an sbt plugin which creates the project structure and can compile to Android bytecode.

Re: Scala on Android

That's good to hear!

Haskell can do OOP!

Haskell's overlooked object system:

"In a first phase, we demonstrate how far we can get with object-oriented functional programming, if we restrict ourselves to plain Haskell~98. In the second and major phase, we systematically substantiate that Haskell~98, with some common extensions, supports all the conventional OO features plus more advanced ones, including first-class lexically scoped classes, implicitly polymorphic classes..."

Re: Haskell can do OOP!

I've been aware of that paper for a while, but have never gotten around to reading it. Thanks for giving me the push I needed to do so! Haskell never ceases to amaze me.


Re: OOHaskell

See above comment.


Check out Mozart-Oz

You might want to take a look at Mozart-Oz, another language from the academia. Part of its beauty comes from the fact that is a multiparadigm language rather than just functional-oo.

Re: Check out Mozart-Oz

I'm aware of Mozart/Oz, and I hope someday to get enough time to explore it in depth :-)

August 2010

Powered by