Hi when binding a value with explicit type we use
let x:float = 1
so this to me says, x returns a float
but when binding a function we use
let add (x:float) (y:float) :float = x + y
when I honestly first guessed the syntax to be
let add:float x:float y:float = x+y
obviously this wont work, but it looks nice and seem more inuitive , why was the former syntax chosen over the later
(note also the syntax let add x:float y:float :float = x + y
dont work you need the brackets
That particular syntax was brought over from OCaml, so probably nobody building F# was ever involved in that decision. It might be hard to track down the roots of that decision when it was made for OCaml.
That said, I tend to like the syntax as it is, so I have a guess why that decision was made. I like it mainly because I often look at and think about functions in terms of their type signature. So if I have
let fn (x:string) (y:int) : float = ...
I find it very natural that the type signature is fn : string -> int -> float
.
If fn
was defined as
let fn:float x:string y:int = ...
it would be a little roundabout to have fn : string -> int -> float
. It would just take more mental gymnastics on my part.
Ultimately though I think itβs just a preference thing. There are perfectly valid reasons why someone would prefer the syntax that you proposed, and perfectly valid reasons someone would prefer the existing syntax. I do like how your proposed syntax makes the parentheses unnecessary.
2 Likes