I work with F # 3.1, and today I encounter a compilation error twice that I cannot explain, and you are out of luck looking up.
In the following code:
type OtherClass(value:int, ?flag:bool) =
member this.DoSomething = (value,flag)
and
MyClass(value:int, ?flag:bool) =
let _dummy = new OtherClass(value,flag)
The compiler says that in my MyClass, where I call the call to OtherClass ctor, the flag type is incorrect. In particular, he says that he needs a choice of bool, not bool. However, it is defined as a bool option according to everything I see.
Any idea why the flag is treated like a regular bool and not as a bool option as defined?
Edit:
After further thought, I think I know what is happening. Optional parameters are inside the method, considered as an option, but outside they want the real one. Something changes the cost of the call to the type of option. This means that to do what I tried to do, we need something like this
type OtherClass(value:int, ?flag:bool) =
member this.DoSomething = (value,flag)
and
MyClass(value:int, ?flag:bool) =
let _dummy =
match flag with
| None -> new OtherClass(value)
| Some(p) -> new OtherClass(value, p)
It works, but it seems a bit detailed. Is there a way to pass an option directly to an optional parameter like this without having to make different calls?
source
share