Propagating optional arguments

安稳与你 提交于 2019-12-04 18:15:42

问题


The following code does not compile.

type A(?arg) =
  member __.Arg : string option = arg

type B(?arg) =
  inherit A(arg) //ERROR expected type string but has type 'a option

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?


回答1:


F# spec 8.13.5 Optional arguments to method members

Callers may specify values for optional arguments by using the following techniques:

  • 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"
    



回答2:


Sorry had to test it first: it seems you are right - you have to do the "?" for A yourself:

type A(arg : string option) =
  new (a) = new A(Some a)
  new () = new A(None)
  member __.Arg : string option = arg

type B(?arg) =
  inherit A(arg)


来源:https://stackoverflow.com/questions/7095620/propagating-optional-arguments

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!