I want to define an overloaded member function in F#
member o.m1(?x: int) =
o.m1("bugs bunny", x) // <- error, expects a straight int, but x is int option
member o.m1(s: string, ?x: int) =
42
but the code above fails. I can solve this so:
member o.m1(?x: int) =
match x with
| Some x -> o.m1("bugs bunny", x)
| _ -> o.m1("bugs bunny")
I wonder if it is possible to avoid this switch.
You can do it by explicitly naming the optional parameter, like this:
member o.m1(?x: int) =
o.m1("bugs bunny", ?x = x)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With