Getting Started in Scala

Tuesday, April 05, 2011
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:

  • Type inference for variables. No need to be overly repetitive like in Map[String,String] table = new HashMap[String,String];. The default is that the variable is just the type of its initial value.
  • Declaring final variables with “val”. In Java, you can in principle tag variables as being constants by adding the “final” keyword, but in practice, I never see myself doing this because it is too much typing overhead. In Scala, I start with everything being a “val” and use “var” variables only if I really must.
  • Functional data structures. By this I mostly mean data structures which provide methods like “map” or “filter”. Much easier than writing a whole bunch of for loops.

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.

  • IntelliJ IDEA, Community Edition In my opinion, this is the best solution. Install the Scala plugin gives you a really good Scala support. The Community Edition also gives you sufficient support for Java, web development, etc.
  • Netbeans with erlybird Scala plugin I’ve tried this for some time, no idea what the current status is. Last time I checked Scala support was quite good, but every once in a while, the parser had to be restarted.
  • emacs. A special Scala edit mode is contained in the Scala destribution in “…/misc/scala-tool-support/emacs/”. Follow the install description in the README file in that directory.

Build Systems

You can go with either maven or sbt.

  • Maven. Use the maven-scala-plugin which provides everything you need.
  • simple-build-tool sbt is a command line tool which gives support to Maven-like dependency management. It also has a nice mode where it recompiles files whenever they change. This is really useful if you use an editor without code analysis to spot syntax errors.

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:

  • Types are essential for method arguments, and when you exit a function using “return.” Everything else is optional. (I’m simplyfing, of course.)
  • There is no throws clause. (But there is an annotation for that if you really need to.)
  • If you override a method, you have to say “override def”.
  • Things you should check out: Pattern matching, “apply” method in the companion object, implicit conversions, Scala collections (in particular immutable collections.)
  • For Scala 2.8.x, type “import scala.collection.JavaConversions._” to use methods like “map” and “foreach” on Java collections.

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 +0000

blog comments powered by Disqus