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"?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With