Grenade Sandwich Explosive thoughts on code, technology and life by Steven Merrill

Scala, Lift, and the Future

Posted on 26 Nov 2009. Filed under .

I've been spending a decent amount of my after-hours time investigating a combination that I think will be part of the future of web programming: the Scala programming language, and the Lift web framework on top of it.

A number of high-scale, distributed systems have recently adopted Scala. Twitter's messaging queues are now handled by Scala and its Actors library. Another high-scale social game, Foursquare, uses Scala for its backend, and Lift for its web tier. (I'm excited to see a presentation about Foursquare's use of Lift at the New York Scala Enthusiasts meetup this weekend.

Rasmus Lerdorf is the inventor of PHP and author of a famous presentation at OSCMS 2007 that exposed flaws in PHP CMSs (although it showed Drupal to be light-years ahead of its open-source brethren in terms of security.) He recently cast a critical eye on Foursquare:

Twitter / Rasmus Lerdorf: Four stars to @foursquare ...

Why I Like Scala

It's got an interactive shell

Like Python, you can run commands in Scala's interactive shell, the REPL (read-eval-print-loop.) Don't take my word for you - you can also play with the REPL online. Another benefit to this is that you can test Lift apps and interactive with its ORM from the command line through the REPL, just as you can with Django through the Python interactive shell.

Scala runs on the JVM and interoperates with Java libraries

Any of the wealth of Java libraries can be used with Scala. If you're already running SOLR on Tomcat or Jetty, you can run a Scala application. Enterprises who already have Java infrastructure can move to Scala apps today.

You can write incredibly concise code with Scala

With its support for pattern-matching, structural types, and just through the general design of the language, you'll find that almost none of your Scala code is boilerplate. Consider the following Java:

public class Person {
  public final String name;
  public final int age;
  Person(String name, int age) {
    this.name = name;
    this.age = age;
  }
}

Next, the equivalent Scala.

class Person(val name: String, val age: Int)

This was taken from a presentation by David Pollak, the creator of Lift. (You can check out the whole presentation along with more comparisons, along with some very low-level Java bytecode.)

Scala supports functional programming techniques

Functional programming will be the way of the future in a world of decreasing processor speeds and increasing numbers of processor cores. Note that Scala merely supports functional programming; it's also entirely possible to write regular, imperative Java-style code as you get used to the functional, Scala idiom. Hey - at least your Java-in-Scala code will still be concise. Once you get used to functional style with no shared state and immutable variables, you can refactor your old code.

Scala also has a built-in actors library which allows for share-nothing message passing between processes, threads or servers. (Facebook chat uses Erlang, which has a similar actor library, to send nearly a billion messages a day.)

Lift is feature-laden and secure

Lift (like Drupal) has many layers of security built in - form tokens, explicit escaping of all values going into templates by default, and the like. See Rasmus's comment above.

A common Lift demonstration is writing a Comet chat application in under 20 lines of Scala code. (Check out slides 9, 10, and 11 of one such presentation.) Combined with a Java web application server like Jetty that supports continuations, actors that are handling Comet long polling can suspend without tying up a thread.

Scala uses Drupal for their site

The Scala webmasters have good taste in their choice of other open-source projects. What can I say?

The Future

So what's the point of all this?

I've been looking around at frameworks as a way to broaden my horizons. I checked out Django and Python briefly, but I think that Scala and Lift could be the next great building block for high-scale social applications. I'll be posting about it as I go. Tomorrow, I'll post a conversion of Peter Norvig's 21-line python spell-checker into Scala.