Function is not an expression

Coming from SML world I killed the notion of the function as an expression, because implicit evaluation of the expression (and thus — a function) can lead to undesired behaviour. Probably the best examples come from Scala forums, where it is asked what happened with if-no-else expression — it is not a proof, but solid hint, that some people were taken by surprise.

One could say it is solely Scala issue, because of snowball effect — the aforementioned function-expression notion and inference of result type — but the less surprises in code, the better. Besides, not only writing code matters, but also maintaining it. And actually seeing “here, here, it is the result of the function” is much clear than doing manual parsing and evaluation in the head.

However there is one exception… if there is no big code, because entire function is simple expression. In case of such one-liner function could be an expression without loss of readability.

Thus in Skila a function is either single expression:

def foo() : Int = addThis(5,2);

or a list of statements:

def bar() : Int
  var x : Int = addThis(7,3);
  return x+10;

but in both cases the result type is not inferred and has to be given explicitly.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: