Assignment of functions and types

In Scala, a functional application is associated on the left, and function types are associated on the right (scala week second week). I do not understand this. Can someone explain?

What is the difference between a "functional application" and a "function type"?

+4
source share
1 answer

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

For example, a function application in Scala is an expression of type 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 

A function type in Scala is a type of function object. Scala has its own specific designation for these types. In this notation, the => operator is used. For example, String => Int is a type of function that takes String as an argument and returns Int .

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

In other words, should you read String => Int => Float as (String => Int) => Float or String => (Int => Float) ? If the operator => was left-associative, then this would be (String => Int) => Float . If it were correctly associative, it would be String => (Int => Float) .

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

+8
source

All Articles