It seems that people coming from languages ββof the C-family (C #, Java, C, C ++, JavaScript) are faced with problems understanding the use of parentheses in F #. Of course, I was, and it took me several years to find out how everything works.
In short, the most basic building block in F # is value. Values ββcan be let -bound:
let foo = bar
This means that foo is a value that turns out to be equal to bar .
Functions are also values:
Here f is a function that takes some value ( x ) and returns a tuple with x both the first and second element.
It's a little cumbersome to write, so there is a shortcut for this:
Note that there are no parentheses in these expressions.
Sometimes you need to prioritize operators. As in mathematics, 1 + 2 * 3 (which is equivalent to 1 + (2 * 3) ) does not coincide with (1 + 2) * 3 . In F #, you also use parentheses to override priority. In this way,
does not match
(in this case, someOtherFunction is a function that returns a string .)
Note that parentheses do not indicate a function call; they are only there to control the evaluation order.
Sometimes you want to define a function that does not accept any data. However, you cannot define it like this:
let f = whatever
because it will make it a value that immediately let -bound on whatever . Instead, you can let the function accept the value of the built-in type unit . This type has only one value, which is written () :
let f () = whatever
This means that f is a function that corresponds to its entry against the only known unit value.
Whenever you call f with () , the expression whatever evaluates to and returns.