My customary warning on these posts: I am absolutely not a F# expert – these posts are about things that caught me out or surprised me as I begin to learn F# as an experienced C# developer. You can find part 1 here.
This is something that’s caught me out a lot while picking up F#. A typical function in F# might look like this:
let multiply valueOne = let userInput = int(System.Console.ReadLine()) valueOne * userInput
Now how about a parameterless function, is it this:
let multiply = let valueOne = int(System.Console.ReadLine()) let valueTwo = int(System.Console.ReadLine()) valueOne * valueTwo
In short: no. This is not a function – the code will execute once and the result be assigned to the immutable constant multiply. To make this a function you need to add parentheses:
let multiply () = let valueOne = int(System.Console.ReadLine()) let valueTwo = int(System.Console.ReadLine()) valueOne * valueTwo
As both are perfectly valid syntax the compiler will process them without warning, it has no way of divining your intent. In this case you’ll probably notice the issue because of the need for user input but I had a really puzzling situation with the setting of the consoles foreground colour which never seemed to quite do what I expected. Needless to say – I’d made this mistake!