Tuesday, October 19, 2010

Scala Tutorial Part 1 - Three Reasons To Learn Scala

In this second part of the scala tutorial series I am going to show you three features that make scala an extremely powerful language.
These features are presented using simple one-liners. The code shown in this post can be run using the scala shell. Running scala code snippets using the scala shell is described in the first part of this scala tutorial series.

To get started fire up a scala console:

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

Static Typing

In the first step we simply define a variable:
scala> var i = -3
i: Int = -3 
Even in this simple statement you can see one reason why scala is such a powerful language: it has type inference and static typing.
You defined a variable named i with the value -3. The scala console shows you the result of this expression: a variable i with value -3 of type Int was created. The type inference system automatically determined that -3 is an integer. The scala compiler uses this information to ensure that only int values are assigned to the variable i. Let's try this out:
scala> i = "The static type system will not allow this" 
:6: error: type mismatch;
 found   : java.lang.String("The static type system will not allow this")
 required: Int
       i = "The static type system will not allow this"
In the above statement i tried to assign a String to our Int variable. As you can see, this results in an error.

Sometimes the code can be more readable if you include the type of a variable in the variable definition. Of course you can do this, but you don't have to. You could also have written:
scala> var i: Int = -3
i: Int = -3 
As you can see, the type of a variable can be declared after the name of the variable. The type declaration consists of a colon and the type name.


// A little side note:
// the book "Object-Oriented Type Systems" by Jens Palsberg
// and Michael Schwartzbach provides excellent background
// information on the topic type inference.


Concise Syntax

One more thing you can see in the above example is the conciseness of scala code. The scala syntax allows you to reduce the amount of boilerplate code you have to write.

You already learned that you can omit the type declaration. But there is one more thing to see in the above statements: You do not have to terminate statements by a semicolon. At least this is true for most cases - in some cases the compiler cannot infer by itself where a statement ends. In this case you have to help the compiler. But in most cases you are allowed to terminate statements by a semicolon.

As you learn more features of the scala language you will learn to appreciate the conciseness of scala. Here is a little teaser:

To define a Map with some values in scala you can use the following code:
scala> var capitals = Map("Italy" -> "Rome", "Germany" -> "Berlin")
To achieve the same in java you have to write the following code:
Map capitals = new HashMap();
capitals.put("Italy", "Rome");
capitals.put("Germany", "Berlin");
In Java you have to define the type of this map twice! Even if you use third party libraries like google collections from the guava project the java code will still be more verbose than the equivalent scala code.


Scala Is Purely Object Oriented

The third and last power-feature of scala i want to show you in this post can be seen in the following code snippet:
scala> 15.toHexString
res0: String = f
What is happing here? You invoke the method toHexString on the number 15. This is possible because everything in scala is an object. Scala is completely object oriented. It does not have simple types like int. Because of this, the literal 15 defines an object of type Int. Of course you can invoke methods on this object.

Conclusion

This post presented three extremely powerful features of the scala languages:
  • static typing and type inference
  • concise code
  • pure object-orientation

Hopefully I got you interested in scala. Stay tuned for the next article in this series!

No comments:

Post a Comment