fsharp中参数和接口继承的缺陷

nic*_*las 4 f# unit-type

有什么理由说KO不起作用?

  type IBase = 
      abstract member test : (unit * unit) -> unit

  type OK() = 
      interface IBase with

        member x.test ((titi,tata)) = () //OK

  type KO() = 
      interface IBase with

        member x.test (titi,tata) = () //fail : This override takes a different number of arguments to the corresponding abstract member    
Run Code Online (Sandbox Code Playgroud)

Mar*_*ann 8

因为括号在F#中表示某些东西(它表示一个元组),所以它们不一样.

如上所述,该test方法被定义为将两个值的元组unit作为参数的方法.如果使用Reflection来获取MethodInfo,则该方法定义为:

Void test(System.Tuple`2[Microsoft.FSharp.Core.Unit,Microsoft.FSharp.Core.Unit])
Run Code Online (Sandbox Code Playgroud)

这与OK方法匹配,但不符合KO方法.

如果您重新定义接口

type IBase = 
    abstract member test : unit * unit -> unit
Run Code Online (Sandbox Code Playgroud)

然后OK不编译,但KO确实如此.

这个替代版本生成一个带有test两个参数的方法:

Void test(Microsoft.FSharp.Core.Unit, Microsoft.FSharp.Core.Unit)
Run Code Online (Sandbox Code Playgroud)


Mat*_*igh 6

abstract member test : (unit * unit) -> unit
Run Code Online (Sandbox Code Playgroud)

括号可用于对复杂参数进行分组,例如,当函数类型是参数时,或指示何时将元组视为单个参数而不是两个参数.http://msdn.microsoft.com/en-us/library/dd483468.aspx

由于括号,您的抽象方法需要一个参数,该参数是2个单位的元组,而不是2个单位参数

您可以将抽象方法定义为:

abstract member test : unit * unit -> unit
Run Code Online (Sandbox Code Playgroud)