Why do these assignments give different results?

Why

let ab ab = a 5 + b 

will create

 val ab : a:(int -> int) -> b:int -> int 

and

 let ab2 ab = a 5 +b 

will create

 val ab2 : a:(int -> int -> 'a) -> b:int -> 'a 

Why does this one space between '+' and 'b' make this difference?

+6
source share
1 answer

The thing is how the parser extracts syntax parameters to avoid ambiguity.

+ is both a binary addition operator and a unary "positive" operator 1 . 5 + b is thus the application of a complement to two arguments; but +b is a positive operator applied to some character b .

In this way,

 let ab ab = a 5 + b 

analyzed as:

 let ab ab = (a 5) + b 

with a is a function of a single integer argument that returns int, so it can be added to b ; but

 let ab2 ab = a 5 +b 

analyzed as:

 let ab2 ab = a (5) (+b) 

with a is a function of two arguments, without the ability to deduce the type it returns.

1 I do not have a list of F # statements, so I canโ€™t verify the correct name. Edit: it looks like I couldn't remember correctly: Arithmetic operators (F #) : -).

+10
source

All Articles