F# 使用多个可选参数包装方法

F# 使用多个可选参数包装方法,f#,F#,我有一个C#类,它有一个方法,该方法有几个可选参数,可以抛出异常。我希望捕获异常并返回结果,同时保持提供可选参数的能力。有没有一种方法可以在不在匹配表达式中创建大量大小写的情况下使用多个可选参数来实现这一点 type SomeType with member this.DoSomethingResult(?a, ?b:, ?c) = try let x = match a, b, c with

我有一个C#类,它有一个方法,该方法有几个可选参数,可以抛出异常。我希望捕获异常并返回结果,同时保持提供可选参数的能力。有没有一种方法可以在不在匹配表达式中创建大量大小写的情况下使用多个可选参数来实现这一点

type SomeType with
    member this.DoSomethingResult(?a, ?b:, ?c) =
            try
                let x =
                    match a, b, c with
                    | None, None, None -> this.DoSomething()
                    | Some a, None, None -> this.DoSomething(a)
                    | Some a, Some b, None -> this.DoSomething(a, b)
                    | Some a, None, Some c -> this.DoSomething(a, c = c)
                    | Some a, Some b, Some c -> this.DoSomething(a, b, c)
                    | None, Some b, None -> this.DoSomething(b = b)
                    | None, Some b, Some c -> this.DoSomething(b = b, c = c)
                    | None, None, Some c -> this.DoSomething(c = c)

                Ok x
            with ex -> Error ex.Message
案例的数量与参数数量的阶乘成正比,因此对于超过3个参数来说,这显然不是一个可行的模式,即使是这样也会推动它。 我考虑过使用
defaultArg
,只调用包含所有参数的内部方法,但我不一定知道包装方法的默认值是什么(或者是否有方法提取它们)并且不想改变默认行为。

答案提供了解决方案的提示,但我找不到专门解决此问题的问题。诀窍是在内部方法调用中使用可选的命名参数:

type SomeType with
    member this.DoSomethingResult(?a, ?b:, ?c) =
            try
                let x = this.DoSomething(?a = a, ?b = b, ?c = c)
                Ok x
            with ex -> Error ex.Message
找到解决方案后,也很容易找到相关的解决方案