Fed up with Java I started to learn Scala. It’s a language that seems to solve almost all problems I spotted in Java. I started with a tutorial; it’s definitelly not a very good-written one (comparing to, say, Java Tutorials or Python Tutorial), but it’s also the best I have found so far.

One of the Java disadvantages that Scala solves is support for functional programming. While programming in Java I often miss functional features, especially closures and first-class function/method objects, but higher-order functions and currying can be also very useful in solving some problems. After reading the 4th and 5th chapter of the mentioned tutorial, I learned how to use those functional features in Scala.

I have to say that Scala is not as functional as, for instance, OCaml is. Let’s analyze the following, simple example of OCaml code, making use of currying:

let f a b = a + b;; let g b = f 3 b;; f 3 2;; // 5 g 2;; // 5

The equivalent code in Scala looks as follows:

def f(a:int)(b:int) : int = a + b; val g = f 3 _ f(3)(2) // 5 g(2) // 5

Let’s take a closer look at differences between the two examples.

In OCaml, each function is curryable by default, without any additional effort. If a function is called with less parameters given than needed, the result always is a function. It’s a simple rule.

Things are different in Scala. If function should be curryable, it has to be properly declared. Each “currying point” is denoted in parenthesis: in the analyzed example this is only one such point (after the first parameter). Moreover, each parameter that could be curried has to be given in its own parenthesis. Well, perhaps it doesn’t look so bad, but let’s suppose we have a function with N parameters (N>0). While the OCaml code is simple:

let f a1 a2 ... aN = ...;; let g a1 a2 ... aK = f a1 a2 ... aK;; // K < N f 1 2 ... N;; g K+1 K+2 ... N;;

the Scala code becomes more verbose:

def f(a1:int)(a2:int)(a3:int)...(aN:int) : int = ... ; val g = f a1 a2 ... aK _ f(1)(2)...(N) g(K+1)(K+2)...(N)

Of course the same function can be written in a simpler form, but with the price of being not curryable:

def f(a1:int, a2:int, ..., aN:int) : int = ...; f(1,2,...,N)

As we can see, there is a clear distinction between curryable and not curryable functions in Scala. The difference is in function declaration, as well as in function calls. This difference makes use currying in Scala a slightly less convenient than in functional languages (like OCaml).

One question comes to mind here: is it a serious Scala disadvantage? Well, everything has its price. As far as I know Scala was meant to be a language which solves many problems of Java, and so far I think it is successful in this field. The example with function taking N paramaters is rather an academical one; it’s a case I proposed to show the fact that currying in Scala is not so convenient as currying in OCaml (and, probably, in other functional languages as well). I doubt I would ever write a fully curryable function of more than 3-4 parameters in Scala, so the “disadvantage” discussed here is rather a small problem in practice…

However, there’s alway the other side. Scala creators seem to focus on functional programming aspects of Scala; these features are the ones which are exposed and emphasized (at least I have such an impression). So if we take a closer look at what they have already done: a programming language with support for functional programming, with not-so-convenient currying… It might seem a little weird.

Anyway, Scala is an interesting language. I’m going to keep learning it further.