citykid
citykid

Reputation: 11070

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.

Upvotes: 4

Views: 178

Answers (1)

Brian Berns
Brian Berns

Reputation: 17038

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

    member o.m1(?x: int) =
        o.m1("bugs bunny", ?x = x)

Upvotes: 6

Related Questions