以下代码无法编译。
type A(?arg) =
member __.Arg : string option = arg
type B(?arg) =
inherit A(arg) //ERROR expected type string but has type 'a option
我认为这是因为必须提供选项的底层类型的实例,并且编译器处理传递
Some
/None
基于语法。假设我的假设已被正确假设,是否有解决方法?是否可以传播可选参数?
最佳答案
F# 规范 8.13.5 方法成员的可选参数
Callers may specify values for optional arguments by using the following techniques:
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"
关于inheritance - 传播可选参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7095620/