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:
- The first thing I noticed was the lack of semi-colons ! Inferring the end of a statement is a job that the Scala parser has taken up, with semi-colons being used mainly to distinguish separate statements in a single line.
- The
package
declaration is familiar, with the added coolness that the physical location does not restrict the package name. object
is a new keyword for Java programmers. It simply indicates that the class defined is a singleton. However, since there is noclass
declared, you can't make additional objects of this type, and hence it is called a standalone object. Scala doesn't havestatic
members for classes, so standalone objects are useful to collect utility functions, or in this case act as an entry point to the application.- Types are declared after the variable name related to the type (
args: Array[String]
), and generics are indicated using square brackets. - The
def
syntax defines a method (a member function) named main, which takes a string array of arguments and returns aUnit
type, which is the equivalent of Java'svoid
. In Scala, you can pass around functions as arguments and there are a number ofFunction
types that make this possible. So a good practice would be to keep your terminology correct and don't refer to a method as a function. - An interesting point is that you can leave out the
: Unit =
part, and the return type is assumed to beUnit
by the compiler. - Another interesting fact is that unlike
void
in Java,Unit
has a value()
, implying you can actually print it out!.
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 2.9.0.1.
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.
Conclusion
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 !