Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to forward an optional argument in F#

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.

like image 782
citykid Avatar asked Oct 19 '25 02:10

citykid


1 Answers

You can do it by explicitly naming the optional parameter, like this:

    member o.m1(?x: int) =
        o.m1("bugs bunny", ?x = x)
like image 166
Brian Berns Avatar answered Oct 21 '25 06:10

Brian Berns



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!