Sunday, November 18, 2012

Should You Learn Scala?

It depends on what you want to accomplish*, but the answer is probably yes.

By way of background, Scala is a programming language originally developed at the Swiss university EPFL in an attempt to apply recent innovations in programming languages research to a language that can gain mainstream traction.  Researchers keep coming up with clever ways to it easier for programmers to write correct code and to write less code, but much of the time only academic programmers benefit because these features are implemented in boutique languages (for instance, OCaml and Haskell) that are not in widespread use in industry or open source projects.

On its own, without considering features such a libraries and interoperability, Scala is a nice language.  It is "multi-paradigm," supporting object-oriented programming and functional programming idioms.  Scala allows you to define types associated with both data attributes (with classes behaving like C++/Java classes) and behavior attributes (with traits corresponding to Java interfaces).  In the way of functional programming idioms, Scala functions are first-class (which means you can pass them around as values) and Scala also supports anonymous functions and currying (partial application of multi-argument functions).  This combination of features make it possible to write Scala programs that are quite concise and elegant**.

Scala's libraries and interoperability make it a force to be reckoned with.  Scala runs on the Java Virtual Machine (JVM) and can interoperate nicely with Java code.  This means that you can use Java libraries directly from Scala code.  It is also possible to call Scala code from Java (see here).  There is a nice post about how you can take advantage of Scala parser combinators, which use higher-order functions to help you write concise parsers, to write parsers for systems otherwise written in Java.  Not only can you do all of the things in Scala, you can easily write part of your program in Scala and the rest in Java.  (This is huge for mainstream adoption potential because so much of the world's code is written in Java and so many developers are taught in school!)

Learning Scala will make you more marketable.  Many companies are using Scala these days, including LinkedIn, FourSquare, Twitter, and Quora.  The founders of Quora spent a couple of months building a Scala web framework in order to use Scala.  Martin Odersky, the man behind Scala, is now behind the startup Typesafe for supporting Scala development.  There are some nice Scala web frameworks out there, including Lift and Play.  Scala is also gaining traction in the research community.  Many more people cared about our research on the Jeeves programming language for automatically enforcing privacy policies once we implemented it as an embedded domain-specific language in Scala.

Scala is also relatively easy to learn, especially compared to other strongly statically typed languages (OCaml; Haskell).  I have previously said that Python is the most marketable language for beginners, but I am beginning to change my mind.  Like Python, Scala has clean syntax, nice libraries, good online documentation, and lots of people in industry using it.  Unlike Python, Scala also has a static type system that can prevent you from doing bad things (whereas in Python the bad things just happen when you run the program).

If you are teaching a course using Java, you may want to consider switching to Scala.  I have recently had positive experiences teaching Scala to MIT undergraduates during recitation in 6.005, the software construction course taught primarily in Java.  During three recitations I introduced how to use functional idioms, how to build an evaluator for a simple language by using data types and pattern matching, and how to build an incredibly concise parser for the tiny language using parser combinators.  You may find my notes here, along with the accompanying code examples.

If you are considering the merits of Scala over other languages, you may also be interested in reading my Quora responses to the questions "What makes a good programming language?" and What are the advantages of Scala over C++ and Haskell?.

* If you are building scientific applications, a language with more math support (Matlab; Mathematica; R) may be superior.  If you just want to do a weekend project writing a Craigslist scraper, Python may be better because it is easier to learn and has great web scraping libraries.
** Okay, Scala programs are not as beautiful as OCaml or Haskell (I have another post on ML vs. Haskell) but it's so much more practical in many ways.

485 comments:

«Oldest   ‹Older   801 – 485 of 485
«Oldest ‹Older   801 – 485 of 485   Newer› Newest»