本文介绍了传播可选参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
以下代码无法编译。
type A(?arg) =
member __.Arg : string option = arg
type B(?arg) =
inherit A(arg) //ERROR expected type string but has type 'a option
我认为这是因为必须提供该选项的基础类型的实例,并且编译器处理传递 Some
/ 无
基于语法。
I assume this is because an instance of the underlying type of the option must be provided, and the compiler handles passing Some
/None
based on syntax.
假设我的假设已被正确假设,是否存在这个解决方法?是否可以传播可选参数?
Assuming my assumption has been correctly assumed, is there a workaround for this? Is it possible to propagate optional arguments?
推荐答案
F#规范8.13.5方法成员的可选参数
F# spec 8.13.5 Optional arguments to method members
- 按名称,例如arg2 = 1.
- 通过按名称传播现有可选值,例如?arg2 = None或?arg2 = Some(3 )或?arg2 = arg2。这在构建一个将可选参数传递给另一个的方法时非常有用。
-
使用与位置匹配的普通,未命名参数。
- By name, such as arg2 = 1.
- By propagating an existing optional value by name, such as ?arg2=None or ?arg2=Some(3) or ?arg2=arg2. This can be useful when building one method that passes optional arguments on to another.
By using normal, unnamed arguments matched by position.
type A(?arg) = member __.Arg : string option = arg type B(?arg) = inherit A(?arg = arg) printfn "1. %A" (B()).Arg // None printfn "2. %A" (B("1")).Arg // Some "1" printfn "3. %A" (A()).Arg // None printfn "4. %A" (A("1")).Arg // Some "1"
这篇关于传播可选参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!