Monday 10 August 2009

Two in One: functional plus OO-programming in Scala


Photo: PyEuler

I believe in the functional first approach to teach computer programming to computer scientists. This means that students start with a functional programming language before programming in an imperative object-oriented style.

The advantage of this approach is that at the beginning the students can focus on problem solving writing functions with a given set of data structures ignoring the computer architecture. Only later they concentrate on how to implement such data-structures in an efficient way by exploiting the computer architecture.

From 1998-2001 I was involved in such a course to first year students of TU Graz. We started with SML in the first half-year  and then switched to Java. All this could now be done in the programming language Scala that combines the pros of both programming paradigms. 

I recommend to take a closer look at Scala. It might be the Java of the future. Scala combines features of both worlds. The most prominent features I've missed so long in OO languages are there:
  • functions are values
  • pattern matching
  • higher order functions
  • closures
  • lazy evaluation
  • Milner's type inference system
However, it is far from being un-orthodox with OO features like:
  • all values are objects
  • compiles to the Java Virtual Machine
  • full overloading including operators
  • (multiple) inheritance
  • direct definition of (singleton) objects 
  • support for concurrency
The power this combination of paradigms brings cannot be underestimated: Scala's control structures can be extended in the language itself. Hence, it is possible to write domain specific commands. An example is Scala's support for the Actor communication model known from Erlang. It has been defined purely in Scala and is included as a library. Voila! Parallel programming with Actors on the JVM.  Use the summer break to dive into Scala! Fun included.