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.

Geen opmerkingen: