maandag 21 december 2009

Move

Hi all,

It has been ages since I last posted anything. This post will actually be the last one on this blog. I have moved to Wordpress to have a bit more flexibility in blogging. To make up for the long delay, I have posted a few entries already to start with.

Please follow me to the new blog.

Regards,
Hugo

maandag 26 januari 2009

Java and the Future

The release of Java SE 7 heats up a lot of discussion. This gave me reason to think more about what Java can do for me as a developer. Let me take one example. Java 7 will not contain closures. Well a lot is said about this topic, and I do not want to repeat this discussion. However, it did show me an aspect of Java, which I was not aware of. 

The thing is, Java is based on a lot of history by now. This also means a lot of design mistakes. There are many insights on how things could have been better. There are missing features in the language, like closures. There are also awkward standard libraries, which could be redesigned much better. This is not at all criticism towards the designers of Java, it is just that people learn as they go along the way. 

There is also the tendency in software development to combine imperative and functional programming styles and get the best of both worlds. 

I think that it is time to get a new start. Either break with history and let Java develop without the burden of the older releases, breaking backward compatibility, or get a new promissing language and start from scratch. 

The second alternative seems to get some body by the fact that SpringSource, the company behind the well known Spring framework, has bought company the Groovy and Grails framework. Groovy is a language that is compatible with Java, so all old code is still usable, but the language is better designed and more complete. 

Because one can retain the backward compatibility with older Java based systems, I think the second alternative is actually the better solution. Anyway, it will be difficult to make Java a more functional language. I think the coming years might get very interesting as languages like Groovy and Scala and may others get higher popularity. It will probably require higher skills from the developers, to master functional programming and imperative programming. At the other hand, we can safely stand on shoulders of the giants that explored a lot of territory for us. 

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.