Scala is wildly multi-paradigm

(written by lawrence krubner, however indented passages are often quotes). You can contact lawrence at: lawrence@krubner.com, or follow me on Twitter.

I had to work with Scala for a few weeks at Timeout. I felt it lacked stylistic clarity (what is idiomatic Scala code?). I notice that most programmers who have been exposed to Scala and Clojure will either prefer Scala or Clojure — I have not yet met anyone who liked them both. Scala or Clojure are opposites. But why do programmers divide over this issue? I was leaning toward the idea that it had something to do with types, with some programmers liking algebraic types (and therefore Scala) and others have a philosophical attraction to the lambda calculus (and therefore Clojure) but there is another possibility: maybe the difference is one of messiness versus clarity. Or, to put that more charitably, Scala offers freedom because it is wildly multi-paradigm, whereas Clojure appeals to purists who want to stick to one paradigm.

The idea of Scala as a mess of conflicting ideas is made well in this comment:

I think that as a research language Scala is very important. It meshes features found in many different languages (OO types, algebraic types, duck-typing, macros) into one compiler, and so teaches us a lot about how these features can interoperate.

But Scala wishes to be a practical language as well. As such, I think it is an exceptionally badly-designed language (which does not, in itself, merits bashing) that masquerades, or claims to be, a well designed one, and herein lies the problem.

As Rich Hickey so eloquently puts it in one of his talks[1], a programming language is like a musical instrument. Every instrument has properties that introduce constraints. A trumpet can only play one note at a time, while a piano has very limited sustain. These constraints do not limit the artistic expression of the instrument – they assist it. An instrument with no constraints is a “choose-a-phone”, and as Rich Hickey says, no one wants to play a choose-a-phone. That is not to say that a rich piece of music cannot be composed for a piano-and-trumpet ensemble. In fact, the JVM, which Scala targets, is the perfect language-ensemble tool, as it offers a harmonic interoperation of different languages of different kinds.

Scala is the ultimate choose-a-phone language, with little or no constraints. When it removed the constraints by mixing all these disparate features, it lost most of their underlying justifications. It adopted JavaScript’s duck-types but lost the simplicity of the language; it adopted Haskell’s algebraic types, but lost the control over side-effects; it adopted Java’s OO types, but again, lost their static-type simplicity. But JavaScript uses duck typing because it can keep the language itself dead-simple and easy; Haskell uses those sometimes-hard-to-grasp algebraic types because they offer safety and carefully controlled side-effects; Java has a basic OO type system because it offers some safety while keeping the language relatively simple. Scala takes these features but drops their reasoning; in the process it lost its coherence. So the challenge of programming Scala turns from the challenge of simply expressing your artistic ideas with your chosen instrument into keeping yourself in line without those helpful constraints. It makes playing a harmonious melody difficult.

Scala masquerades as a well-designed language because it includes all of these modern, cool, features. Each of these features is well designed. But putting all of them together is the opposite of good design. The whole, in this case, is much less than the sum of its parts.

All this is not purely philosophical. Languages like Clojure and Erlang tell the programmer “this is how you should write software”, and if you need to do something else, you should integrate Java or C code. Scala, on the other hand, offers little guidance. Immutable and mutable variables are just as easy to declare; the “expression problem” is sometimes solved using OO and sometimes FP. As a research language – that’s great; as a practical language, it can be destructive.

Scala started as a language for research into programming languages, and it is a mess of ideas mostly because the people who created it have the mindset of people who are tinkering with various ideas, trying out different things, but not committing to anything. Consider this remark from 2007, and notice the emphasis on experimentation:

Posted by Adriaan Moors

Scala makes for a great language to experiment with programming language design. In my upcoming posts, I’ll address applications such as:

1.) parsing — I’ll explain how to get your parser up and running in no time, without resorting to external tools. Scala comes with an easy to use library of parser combinators, which means there’s no extra tool to integrate in your build process, no weird generated code to debug, and no new language to learn. Well, that last part is a bit of a fib, as the library of parser combinators essentially implements a domain-specific language in Scala itself!

2.) variable binding and substitution — Getting substitution right is trickier than most people think. We’ll look at a library that encapsulates all this, and which integrates nicely with Scala’s parser combinators to express variable scoping right in your language’s grammar.

3.) type-checking — We’ll see how you can “port” the inference rules that define your type system from paper to Scala, with most of the boilerplate hidden underneath the covers of a type-checking monad.

As he says, “Scala makes for a great language to experiment with programming language design”, which makes it great for learning about programming ideas, but is it a language you’d want to use in production? If you like Scala, you are free to argue that Scala has matured since 2007, and of course that is true, but please consider the possibility that most languages remain wedded to the guiding philosophy that it started with. Ruby has always had its focus on programmer productivity, to the exclusion of any concern about execution performance speed, and Scala was meant to facilitate experimentation, and it continues to allow a wild mix of styles.

Post external references

  1. 1
    https://news.ycombinator.com/item?id=6838836
  2. 2
    http://www.scala-blogs.org/2007/12/teaser-scala-for-pl-geeksw-researchers.html
Source