Tuesday, September 29, 2009

The path less followed

A couple of months ago, I started my scala journey in earnest. You have probably heard of the many benefits of using scala, if not have a look here. I'd like to give you some pointers on how to get started in scala and send you along your interesting journey of discovery.

Your central source of scala is here. You can download the latest version of scala from here. On linux simply download the .tgz, extract it and add the bin directory to your path.

You can verify your installation by invoking the following from a command prompt:


scalac -version

which will output something similar to:


Scala compiler version 2.7.5.final -- Copyright 2002-2009, LAMP/EPFL

To launch the scala interpreter invoke the following:


scala

This will give you a prompt like the following:


Welcome to Scala version 2.7.5.final (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_14).
Type in expressions to have them evaluated. Type :help for more information.

scala>

Greeting the world as per usual can be done as follows:


println("Hello World")

This will print out the String "Hello World" just beneath the println command. As with java, (System.)println prints out a line of text to the standard out.

We can define a simple method as follows:

def greeter(name : String) = println("Hello " + name + "!")

greeter: (String)Unit

The above means that greeter is the name of the method, which takes a String and returns Unit (or void in java).

It can be invoked as expected:


greeter("Dolly")

Which would give:


Hello Dolly!

You can write any method you like in the interpreter and execute it.

This is good for small programs but not so great for larger ones.


Let's create our first scala class.

First create a folder called blog, which is essentially our package. Now create a file called SimpleGreeter.scala within the blog package with the following contents:

package blog

object SimpleGreeter {

def main(args : Array[String]) {
if (args.length == 0) println("Usage: SimpleGreeter your_name")
else println("Hello " + args.mkString(" ") + "!")
}
}

This is a simple "main" method with a snippet of code that either displays a greeting based on the arguments supplied or if no arguments are supplied shows the usage.

It's very similar to java's main method definition:


def main(args : Array[String])

public static void main(String[] args)


Go to your source directory and compile the scala class:


scalac blog/SimpleGreeter.scala


If you have a look under your blog package you should see the following classes:


SimpleGreeter.class
SimpleGreeter$.class


We can run the SimpleGreeter with:

scala blog.SimpleGreeter my very long name


We then get the following:

Hello my very long name!

If you call the greeter with no parameters:


scala blog.SimpleGreeter

We get:


Usage: SimpleGreeter your_name


An interesting thing to note is that since scala runs on the VM, we should be able to do this:


java blog.SimpleGreeter my very long name

If we run this we get the following error:


Exception in thread "main" java.lang.NoClassDefFoundError: scala/ScalaObject
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:621)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
at java.net.URLClassLoader.access$000(URLClassLoader.java:56)
at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
at blog.SimpleGreeter.main(SimpleGreeter.scala)
Caused by: java.lang.ClassNotFoundException: scala.ScalaObject
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
... 13 more


What we need to do is include the scala-library.jar library that came with the scala installation into the classpath to make it work. The library is found in the lib directory of the scala installation.

Trying this again:


java -cp .:your_scala_lib_location/scala-library.jar blog.SimpleGreeter my very long name

We get:


Hello my very long name!


This is awesome, because once you compile your scala sources into classes, they are essentially java classes! :) Oh! How sweet it is! :)

I'll leave this introduction there for the moment. Some good resources for further study are:

0 comments: