Sunday, May 31, 2009

Scala really is a "better Java"

I have been so busy this year that I have slacked off on fully getting up to speed on Scala (and Haskell, for that mater). A few people have been working on a Sinatra clone in Scala. (Sinatra is a very nice minimalist Ruby web framework that I like a lot.) I grabbed a version of the Step project off the web today and I just had some time to play with it.

You could not really write something like Step in Java without writing an interpreter. With Scala, you can add syntactic sugar similarly to Ruby. Step nicely emulates parts of Sinatra; here is a code block wrapped to handle an HTTP GET request mapped to the route "/form":
  get("/form") {
Template.page("Step: Form Post Example",
<form action='/post' method='POST'>
Your name: <input name='name' type='text'/>
<input type='submit'/>
</form>
<pre>Route: /form</pre>)
}
With Scala's ability to define operators and deal with code blocks, and native XML support, Step was defined in less than 100 lines of Scala code. Very nice.

9 comments:

Josef said...

Check out the Groovy language. I think it has the potential to slow the moves from "pure" Java syntax and design into things such as Scala.

Not that any language is better then another, just that there is room for many and the advantages in how they address the diverse requirements we face (even developer skills and comfort).

Mark Watson, author and consultant said...

Hello Josef,

Groovy has become a popular language, but if I want a slow scripting language, I prefer Ruby (or JRuby on the JVM).

Scala has a huge advantage over Groovy and JRuby: very good runtime performance (almost as good as Java). Check out language benchmarks comparing these three languages. Ruby has become my default programming language, but I do still go back to Java (or Common Lisp) when I need good runtime performance.

-Mark

fristname said...

if you know Common Lisp have you trieds then Clojure?

Mark Watson, author and consultant said...

Hello Firstname, I have experimented with Clojure, but it is much slower than either Java or Scala. If execution speed is not an issue, I prefer using Ruby (or JRuby).
-Mark

jherber said...

Mark, I've been using Scala off and on now for just over a year. Be careful, it makes Java coding painful :)

I was just out in Sedona (camped near Bear Mountain) - I should have looked you up for a hike and language/AI discussions.

Jim Herber

Javier said...

I think if you take care of java reflection calls in clojure is faster than jruby or ruby, it's homoiconic (code-as-data), it have STM and in general it's more lispy (including macros which allows the compiler to be extended by user code)
I think clojure is a step forward (or back if you look at its 50's syntax) from scala or ruby

Javier said...

And if you keep in mind that clojure is dynamically typed and scala static the difference is not so great
See this discussion about scala vs clojure performance

Mark Watson, author and consultant said...

Jim: Java coding is a little painful, compared to most other languages I use. But, the JVM plaform is great - thus my interest in Scala: very close to Java performance with excellent language features. Also, a hike would have been good!

Javier: Yes, Clojure is somewhat faster than JRuby. BTW, the benchmark you linked to between Clojure and Scala is more than a little bogus: Scala should be compiled (to get tail recursion optimization). Also, the benchmark itself does not seem very 'real world'. The developer of Clozure released (many years ago) an open source bridge between Java and LispWorks - that was very cool. I tink that Clojure is a great idea and implementation - it is just that if I use a Lisp language, I want very good runtime performance (I use SBCL, Franz Lisp, and Gambit-C Scheme).

Best regards,
Mark

Alan said...

Hi Mark, I'm Alan, one of the Step developers. Thanks for trying it out, and for your kind praise. I've been working with Scala for a few months now, and it has yet to rub me wrong.

Speaking to the other comments on this page, I think that taken together as interoperable languages on a common platform, JRuby, Groovy, Clojure, Scala, and Java represent a unified and powerful (maybe even unprecedented) set of tools for the engineer.

So while each has its upsides and downsides, they all share the common ability to work nearly seamlessly with one another. I've been experimenting with a JRuby on Rails/ScalaQuery project that has been surprisingly easy to get off the ground. This, I think, is just awesome.

Thanks again for checking out Step, and I've really enjoyed browsing your posts.

Alan