Question: What’s the difference between
let f ( a : int option, b : string option ) = ...
and
let f ( ?a : int, ?b : string ) = ...
Answer: in the first example, even if you have no values for a and b, you must call
let ret = f ( None, None )
whereas in the second example, you can simplify matters:
let ret = f ()
But what if you want to supply default values for those optional parameters? Here’s an example of a type definition in which the constructor takes optional parameters and assigns them default values if no value has been supplied. defaultArg is an F# operator, not a locally defined function.
type T( ?a : int, ?b : string ) = let a = defaultArg a 42 let b = defaultArg b "Hello, World"
Whilst this syntax seems rather clunky, it is effective. You can read more about it on MSDN (scroll down to the section “Optional Parameters”).