donderdag 27 november 2008

Scala Discoveries Part II - Expressions and Functions

In Part I I talked about Scala in general to give you a feel where it belongs. Now I want to go in a bit more detail. The Object-Oriented and Functional aspects of the language will get more clear when we cover Expressions and Functions in more detail.

Let us first discuss definitions. In Scala there are a few ways to make a definition. Using the keyword def one can introduce a name which stands for a certain expression. For example:


def x = e


In this case the expression e is evaluated every time the name x is used. The expression itself is not evaluated yet at the time of definition. It is evaluated every time when it is used. This type of declaration is called a Function.

Scala is a pure Object Oriented language. Everything is an object. There is no such thing as a primitive type. Since everything is an object, there is no exception for numbers and functions. This is a bit strange from a Java developer perspective, but the following expression is actually a series of method calls:

1 + 2 + 3 + 4 + 5


This expression is actually the same as:

1.+(2.+(3.+(4.+(5.))))


The unusual aspects (for a Java developer) become clear in this example. First of all 1 2 3 4 5 are all objects. They have methods with identifiers +. + - * etc. are all valid identifiers. All this means that an expression in Scala is always a series of method calls, although it might not immediately be recognizable as such. The reason why the first code fragment looks so familiar is because of the use of infix notation. Infix operators are functions which sit in between the object and the parameters. Here follows another example which might make things more clear.

val s = "Hello"
val index = s indexOf 'l'

This actually means:

val index = s.indexOf('l')

The infix notation can only work when a function has one parameter.

Let us now look to a bigger example.

import java.io._
def readFiles(path: String): List[File] = {
val location = new File(path)
if (!location.isDirectory)
List(location)
else {
for {
val dir <- location.listFiles.toList
val files <- readFiles(dir getAbsolutePath)
} yield files
}
}

This method will recursively list all files in a directory. It will take the path as a parameter. If the location is a File, then a List with the File as element will be returned. Otherwise, if the location is a directory, all elements within the directory are checked again with the same method. The for structure first iterates over all the entries in the directory and will return a List of the content of all the entries. yield will assemble all the results of the for loop and appends them to one big list. An interesting feature is, that the return value of this method is the value of the last executed expression, which is either List(location) or for...yield files. Both expressions have a return type: List[File].
As a side feature, you see how a Java library is directly imported in Scala.

We have now talked quite a while about the def declaration. There are two other types of declaration: val and var. val is a value declaration.

val x = "Hello"

Now the value of x cannot change anymore. The behavior is actually the same as a final field in Java. If you want the value of x to change, declare it as a var:

var x = "Hello"


This part has now covered the declaration of functions, values and variables.

dinsdag 11 november 2008

Scala Discoveries Part I - Hello World

This is the first in a series of posts on the Scala programming language. These posts are primarily aimed to force myself to an understanding of the concepts of the language and to get myself up to speed. Therefore this series will reflect my own knowledge of the subject. It is assumed that the reader is familiar with Java.

We will start of with some basic concepts. Scala is a language which tries to combine the benefits of Object-Oriented programming and functional programming. To see what the impact of this statement is, I need to explain these terms a bit further.

Functional programming is a programming paradigm which tries to achieve results purely through functions. This means that there is no maintainance of state. State changes are only represented as functions that transform the state. This is in contrast with imperative programming, where computational results are achieved by state changes. Object-oriented programming is an example of this. Here the state of the application is captured in its objects.

Scala wants to capture the advantage of both paradigms. We will see examples of this when we go along. In fact, on one side Scala is a complete Object-Oriented language. Everything is an object, there are no primitive types and also functions are objects. But on the other side, it is perfectly possible to use for example Functional programming styles for algorithms.

Scala is compatible with Java. All Java libraries and frameworks can be used easily in Scala. This is a big advantage if you want to reuse your exising software. You can just include the Java libraries and usage of these is completely transparant.

Lets now look to the unavoidable HelloWorld example. I took this one from the tutorial on the Scala site:


object HelloWorld {
def main(args: Array[String]) {
println("Hello World!")
}
}



Most of this looks very familiar to the Java developer. One thing that immediately attracts attention is the object declaration. This is actually the declaration of a Singleton object. Scala does not have static methods. If you want to use something like that, you must declare the methods on a Singleton object. Therefore, although it is not immediately visible, the main method is a static method.
Since HelloWorld is an object, it is declared as class and also as an instance, both with the same name. The instance is created as soon as it is used.

Next time I will discuss more about the use of functions.