Close and Go BackBack to Viget

Scala: The Adventure Begins!

Clinton R. Nixon
Clinton R. Nixon, Development Director, September 24, 2008 4

I’ve been enthusiastic about the Scala programming language for a few months now, and this week’s been very exciting for the Scala community. A new Scala book from the Pragmatic Programmers was announced today, and Alex Payne from Twitter gave a presentation at C4 that strongly indicates Twitter’s writing part of their platform in Scala. Given this week’s news, I thought I’d explain why I’m using Scala, and what that means for the Lab.

I first discovered Scala while I was using JRuby. I was flipping through a list of languages for the Java Virtual Machine, and the example of Scala code really intrigued me. I couldn’t tell if it was statically typed or dynamically typed at first, and when I looked closer and found its type inference, I was hooked. I’ve got a wide background in many languages, but almost all of them, outside of C, are dynamic languages, and so I hadn’t seen type inference before. I chose Scala to be my language to learn this year.

Being busy with awesome work projects, “learning Scala” quickly became “kind of playing with it and subscribing to a lot of RSS feeds.” It wasn’t until I had a class on concurrency using Java this summer that I picked Scala up for real again. Trying to design a multi-threaded program in Java is sort of like knee-boarding with a cat, and I remembered that Scala had a better way. Its Erlang-esque actors and message passing system made writing concurrent programs dead simple. If you’d like to see an example, check out these two classes I wrote for a concurrent program moving passengers on and off elevators.

It’s not a completely fair comparison, as the Java program only keeps counts of people waiting while the Scala program keeps a list of the people, but the general idea is the same. Note that every method in Floor.java is synchronized, blocking access to it while another thread is in the method, and there’s a fairly complex set of states, preconditions, and postconditions to prevent race conditions and deadlock. In Floor.scala, we have one private method that waits for messages, deals with the message through pattern matching, and then sends a message back to the original sender. It’s a cleaner, easier to read model, and it’s less likely to contain a hidden race condition.

Concurrency’s not the only reason I’m excited about Scala, of course. Even the most die-hard Ruby enthusiast - I include myself in that description - knows there are some tasks that would perform much better in a compiled language. For MRI, you can always write a C extension; for JRuby, you can write a Java class. Scala’s completely compatible with Java, and it’s possible (although not always completely easy) to call Scala classes from Java, which means you can access them from JRuby. Java’s a fine language, but as someone from a scripting language and functional language background, I’d prefer the first-class functions and type inference of Scala any day.

So what does this mean for the Lab? It doesn’t mean we’re going to use Scala any time soon. This is a personal project of mine. However, we encourage all developers to keep learning and bring those lessons back to the group. We originally switched from PHP to Rails because of Ben Scofield’s interest in and enthusiasm for Ruby. I recently presented on Scala at our monthly developer meeting, and I’m keeping it in mind for the right project where we have to integrate with Java. We know that Rails will not always be the answer. My bet is that the answer might well someday be Scala.

Alex Payne said on 09/25 at 01:05 PM

Great to see more smart folks getting interested in Scala!  If you feel like contributing the occasional tip or tidbit to http://www.gracelessfailures.com/ just let me know.

And yes, we’re writing progressively more of Twitter in Scala.  I’m hoping to share some of our first “official” open source Scala code soon.

Jerry Cheung said on 09/25 at 06:30 PM

Thanks for the writeup, but a quick question:  What is MRI?

Clinton R. Nixon said on 09/26 at 06:44 AM

Jerry,
Thanks for catching that. I should have put the abbreviation in the article. MRI stands for “Matz’s Ruby Interpreter,” the current C implementation of Ruby 1.8.x.

chris said on 10/01 at 05:58 PM

does ColdFusion ever cross anyone’s mind? When integrating with Java it’s a ten (taken it’s core is Java)…

any thoughts?

Commenting is not available in this weblog entry.

We're the Developers

at Viget Labs. We write about web development trends, tips, best practices, industry events, and our projects — all with an emphasis on Ruby on Rails.

Recent Comments

For translating strings you can use Rails I18n backend instead of using inflectors.

The `typus_human_name` is a patch to fix a problem in `human_name` [1].

[1] https://rails.lighthouseapp.com/projects/8994/tickets/2120-humanize-and-human_name-dont-separate-words

Contact Us

Have any questions, comments, ideas, or secrets to share? Let us know.


Sorry, you need to have Javascript enabled to use this form. (Don't blame us, blame the spammers!) If you'd like to contact us, please visit our Contact page.