Tuesday, April 05, 2011

Getting Started in Scala

File under: machine room

Often, when you search around for posts on Scala, you find pretty scary looking posts which discusses some rather advanced topic of the Scala type system or something similar, or just how functional Scala is by reimplementing ideas from Haskell in it.

On the other hand, I’ve been using Scala for more than a year now, and I find the whole experience very agreable, not because of the crazy stuff you can do in Scala (well, at least, not ONLY because of the crazy stuff), but because of the everyday improvements over Java Scala provides. For example:

In any case, here is what’s necessary to get you started with Scala (assuming you already know enough Java and the infrastructure.)

Downloading Scala

Just download Scala, unpack and add the “bin” directory to your PATH.

Development Support

I’ve tried the following three IDEs.

Build Systems

You can go with either maven or sbt.

Learning about Scala

Some more tips to get you started

I found that a great way to start playing around with ideas is to use the script mode of Scala. You can directly run files by typing “scala some-file.scala”. In Scala, you can put as many classes into a file as you want, so why not just do a quick sketch of your ideas in a single file.

For all those people coming from Java, here is a short list of things you should know:

Speeding up Scala compilations

Unfortunately, the current implementation of the scala compiler takes very long to start up (a few seconds). To improve this, the scala distribution comes with a scala server called “fsc” which runs in the background, and you should definitely use it.

In Maven, the default to use fsc is controlled by the useFsc parameter of the scala:cc goal, but the default is true according to the documentation.

In IDEA, you control the setting through “Compiler > Scala > Use fsc (fast scalac)”.

Using fsc also has it quirks. Sometimes, when a lot of classes change or are renamed, it gets confused and needs to be restarted with “fsc -reset”. Especially on Linux, IDEA cannot compile if the fsc isn’t already running. You can vote up the bug, for the mean time, you need to periodically run a small compilation in order to keep fsc from shutting down.

Before I start to work, I start the following script


fsc -shutdown
while true; do
  scala $HOME/helloworld.scala
  sleep 1500

with the file helloworld.scala being the one-liner

println("Did some random run of scala at %s.".format(new java.util.Date))

or something similar.

Posted by Mikio L. Braun at 2011-04-05 16:47:00 +0200

blog comments powered by Disqus