Consider the following code:
type Test () =
member o.fn1 (?bo) = 1
member o.fn2 (?bo) = o.fn1 bo
member o.fn3 (?bo) = 1 + bo.Value
member o.fn4 (?bo) = o.fn3 bo
While fn1
and fn2
work just fine, fn4
produces the following error:
init.fsx(6,30): error FS0001: This expression was expected to have type int but here has type 'a option
MSDN states:
Optional parameters are interpreted as the F# option type, so you can query them in the regular way that option types are queried, by using a match expression with Some and None.
To me, optional parameters are not interpreted as the F# option type otherwise the code would compile. Moreover I do not understand why, when I hover over ?bo
in fn3
the tooltip says val bo: int option
but from outside expects only int
. I would expect a behavior of accepting nothing, int, Some int and None. And as the last note, I do not understand why fn2
works but fn4
does not.
Thanks for clarification
I have to reconsider the correct answer. Based on this question (and answer):
Propagating optional arguments
it seams that the correct answer is the following:
type Test () =
member o.fn1 (?bo) = 1
member o.fn2 (?bo) = o.fn1 bo
member o.fn3 (?bo) = 1 + bo.Value
member o.fn4 (?bo) = o.fn3 (?bo = bo)
It is a neat feature and credits for the answer go to desco!