4
votes

In the below example I'm using Type abbreviation. Why should I use the fun keyword as opposed to calling it without the fun keyword?

type AdditionFunction = int->int->int

let f:AdditionFunction = fun a b -> a + b
1

1 Answers

6
votes

Because otherwise you can't annotate it with AdditionFunction type. You need to put that on a value that has type int -> int -> int, and the way F# syntax works is that you have no way of doing it with the let-bound style.

Your options are:

let f : int -> int -> int = 
   fun a b -> ...

let f a : int -> int = 
   fun b -> ...

let f a b : int = 
   ...    

and only the first one has the type you could replace with the alias.

But ultimately you don't need to do it. You can define it as an int -> int -> int function and you'll still be able to use it anywhere you'd use an AdditionFunction.

At the end of the day, type aliases are just that - aliases. They can be used interchangeably with the types they stand for and are erased during compilation.