2011 June 22

Getting Started with Scala

I have a feeling that learning Scala is going to be a long ride! Since Scala allows you to deal with many theoretical topics, the lack of a background in such areas will hamper your ability to grok a number of code samples on the web. At the same time, there are many resources to get aquainted with Scala syntax and idioms, so it's just a matter of time. But I miss the ability to go copy-pasta gangsta like learning Java, C# or Python ;) !

An excellent guide for Java developers is David Copeland's Another Tour of Scala.

A nice feature of Scala is that you can use it for interpreted scripts as well as compiled programs. Running the scala launcher will start an interpreter, which is a good way to quickly get familiar with the basic syntax. But at the same time, I was also curious about the IDE support, so I installed the nbscala plugin on Netbeans 7. The Eclipse plugin has more official sanction, but since I prefer Netbeans, I just thought of checking out how well-cooked it is. Later, I found out that a lot of people prefer the IntelliJ IDEA community edition with its Scala plugin. Of course, you can simply use your favourite text editor and run scalac to compile the file into .class files, which can then be run using the scala launcher.

Hello World

So back in Netbeans, once you install the plugin, you get new options for creating Scala project. A new project generates a sample hello world program as follows:

package run

object Hello {

  def main(args: Array[String]): Unit = {
    println("Hello, world!")


These are my impressions of the Scala syntax:

Generated Code

Now I decided to dig deeper into the basic classes generated by Scala similar to what was done here. Since the post was a bit old, I decided to redo the exercise for Scala

Once you run the program, you'll find that file above compiles into two classes Hello.class and Hello$.class. And running the disassembler, javap will produce the following output:

/*Compiled from "Hello.scala"*/
public final class run.Hello extends java.lang.Object{
    public static final void main(java.lang.String[]);

/*Compiled from "Hello.scala"*/
public final class run.Hello$ extends java.lang.Object implements scala.ScalaObject{
    public static final run.Hello$ MODULE$;
    public static {};
    public void main(java.lang.String[]);

So we find that the Scala compiler has generated a special class Hello$ for the singleton. The Hello$.main is then invoked by the standard Java main program generated by the compiler.


One of the factors that worry me about new languages and runtimes is the performance. A stackoverflow discussion informs that you can quickly blow up your memory usage when you use idiomatic Scala, due to the large number of incidental objects and classes genererated. However, this trade-off is apparently made palatable by the fact that you end up coding your problem much faster, and hence can profile your application to isolate the performance-critical code.

A good takeaway from this that it is very valuable to understand well what are the implicit operations denoted by a particular Scala expression. Copy-and-paste coding will probably get you into hot soup very soon !