Why is Scala very complex?

If you think the language is complex then you might consider learning it in stages.

This guide to breaking up Scala into different parts is what many people do instinctively and usually necessarily, but this guide is written by Martin (Scala's creator) so it has his unique perspective. http://www.scala-lang.org/node/8610

Notice that most of the constructs that freak people out are in the Library designer levels. The intermediate application developer can get a lot done without too many CS grad-school concepts.

**
            Level A1: Beginning application programmer
                Java-like statements and expressions: standard operators, 
                     method calls, conditionals, loops, try/catch
                class, object, def, val, var, import, package
                Infix notation for method calls
                Simple closures
                Collections with map, filter, etc
                for-expressions

            Level A2: Intermediate application programmer

                Pattern matching
                Trait composition
                Recursion, in particular tail recursion
                XML literals

            Level A3: Expert application programmer

                Folds, i.e. methods such as foldLeft, foldRight
                Streams and other lazy data structures
                Actors
                Combinator parsers

            Level L1: Junior library designer

                Type parameters
                Traits
                Lazy vals
                Control abstraction, currying
                By-name parameters

            Level L2: Senior library designer

                Variance annotations
                Existential types (e.g., to interface with Java wildcards)
                Self type annotations and the cake pattern for dependency injection
                Structural types (aka static duck typing)
                Defining map/flatmap/withFilter for new kinds of for-expressions
                Extractors

            Level L3: Expert library designer

                Early initializers
                Abstract types
                Implicit definitions
                Higher-kinded types
**

@Anantha For the last ten years most universities have been teaching their students Java as first language. I've heard of a strikingly high number of cases where it even remains the only language students get to learn while at college - unless they pick up something else on their own, that is.

Purely from a language viewpoint, Java's three most characterizing features are

  1. it's imperative
  2. it's object oriented
  3. it's garbage collected

Features 1 & 2 make it very similar to a wide array of languages from the Algol/C and C++ family. All of these languages either share similarities in their paradigm or even utilize exactly the same.

C# for instance, despite it's syntactic differences, Windows as main target OS and .NET framework as "class library", is very easy to pick up for a Java programmer. That is due to both languages sharing the same programming paradigm.

Scala on the other hand - despite running on the JVM, providing easy interoperability with Java APIs - is what is commonly referred to as a multi-paradigm language. The language provides deep syntactic integration of functional programming language features, yet structures code in an object oriented fashion.

The concept of functional programming - especially once you get into code beyond trivial tutorials - proves to be hard for devs who are only experienced with imperative, OO languages. My personal experience with helping fellow developers get up to speed on Scala et al is that it greatly helps to teach them Scheme first ;) It's a nice, small, clean Lisp dialect. It helps with conveying advanced functional concepts. If you decide to give it a shot, I recommend you have a look at "The Little Schemer" and "The Seasoned Schemer". Once you're through with both books, I bet you'll have an easier time looking right through Scala's syntax and seeing it's concepts more clearly.

In a nutshell: IMHO it's not Scala that is hard to learn, the functional programming paradigm is the reason for most devs, who've only been exposed to imperative languages, having a hard time getting up to speed.


Scala is complex because it gives you flexibility. Time after time, not having enough flexibility makes it difficult to do some tasks, yet too much flexibility is like too much money, it empowers you to make the really big mistakes.

Scala is both Object Oriented and Functional. Both of these language types were once considered quite complex apart (although object oriented is now more mainstream) but putting them together opens all sorts of new doors. Some of those doors look like short cuts to "mission accomplished"! Some of those doors have lions behind them. Functional programming gives you all the rope to get the job done, hang yourself, and tie your neighbourhood up in knots for years. It's up to you to not injure yourself in a functional programming language.

The key to successful Scala is to recognize that you should be a successful object oriented programmer, a successful functional programmer, and then learn how to mix the two together in ways that get you to your goal. That's a lot of work. Perhaps in the future, people will know what is the "best" approach to learning Scala, but for now, the only approach known is to be good in two different approaches to progamming, AND be good in mixing them together.


There are two questions here:

  1. Is it more difficult to learn Scala than Java?
  2. Is code written in Scala tends to be more complicated than code written in Java?

The first question is easier to answer: The Scala language is richer than Java. In particular, its type system is more expressive than Java which means that one can express more logical errors as compile time errors. However, in order to exploit these capabilities one needs to be acquainted with the different constructs of the language (dependent types, case classes, variance annotations, views, to name a few). Mastering these takes time and that's why Scala is more complicated to learn than Java.

The second question is trickier. Scala advocates claim that these new constructs make it easier to write correct programs and that the resulting code is simpler. Others are saying that Scala's additional power does not outweigh the complexity of understanding the sematnics of its constructs (For example, take a look at this talk. Search for "Scala"). This is a manifestation of broader dispute: that of statically vs. dynamically typed languages.

Tags:

Java

Scala