Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function application and types association

Tags:

scala

In Scala, function application associates to the left while function types associate to the right (scala course week two). I don't get this. Can anyone explain?

What exactly is the difference between "function application" and "function type"?

like image 747
More Than Five Avatar asked Apr 03 '13 21:04

More Than Five


1 Answers

Associativity (left or right), in general, is a predefined property of some notation (operator) which tells us how should we read expressions that use this notation multiple times in a chain.

For example, function application in Scala is an expression like fun(param1, ...). It can be chained, for example: fun(a,b)()(g,h). And the fact that it is left-associative means that such an expression is equivalent to ((fun(a,b))())(c,d), that is (in pseudocode):

( ( fun applied to arguments a and b ) applied to no arguments ) applied to arguments c and d

Function type in Scala is a type of function objects. Scala has its specific notation to denote such types. This notation uses => operator. For example String => Int is a type of function that takes String as an argument and returns Int.

Now, the question arises, what is String => Int => Float? Is it a function that takes as an argument a function from String to Int and returns Float? Or maybe it's a function that takes String and returns a function from Int to Float?

In other words, should we read String => Int => Float as (String => Int) => Float or String => (Int => Float)? If => operator was left-associative, then it would be (String => Int) => Float. If it was right-associative, it would be String => (Int => Float).

As we know, => is right-associative, which means that String => Int => Float is equivalent to String => (Int => Float) and it denotes type of a function that takes String and returns another function that takes Int and returns Float.

like image 54
ghik Avatar answered Sep 21 '22 22:09

ghik