抽象声明中没有参数名称?

Fra*_*ori 26 f# c#-to-f#

这是F#中抽象成员的典型声明:

abstract member createEmployee : string -> string -> Employee
Run Code Online (Sandbox Code Playgroud)

您可以定义参数类型,但不能定义其名称.没有名称,在实现接口时如何判断每个参数是什么?换句话说,您如何知道接口是否希望实现为1-或2-?

1-   member this.createEmployee firstName lastName = ...
2-   member this.createEmployee lastName firstName = ...
Run Code Online (Sandbox Code Playgroud)

我是从错误的角度看待问题(习惯于C#)吗?

Ram*_*nir 39

关于什么:

abstract member createEmployee : firstName:string -> lastName:string -> Employee
Run Code Online (Sandbox Code Playgroud)

  • 有趣的是,将参数名称和类型对包装在括号中是一个错误,就像为函数参数指定类型时一样,例如:`abstract member createEmployee:(firstName:string) - >(lastName:string) - > Employee` (14认同)
  • 谢谢,这有点尴尬:-),我以前从未见过在线文档中名为params的例子. (3认同)

JHo*_*JHo 6

这个语法非常适合IMO.我想用参数作为元组(比如C#方法)这样做,并且只有通过反复试验我才发现这个工作:

    abstract member PutChar : x:int * y:int * c:char * flag:Background -> unit
Run Code Online (Sandbox Code Playgroud)

这个丑陋的变体也有效:

    abstract member PutChar : x : int * y : int * c : char * flag : Background -> unit
Run Code Online (Sandbox Code Playgroud)

以下是所有感觉合理但失败并出现同样错误的事情 - Unexpected symbol ':' in member definition.:

    // ALL BAD vvv
    abstract member PutChar : (x:int * y:int * c:char * flag:Background) -> unit
    abstract member PutChar : (x:int, y:int, c:char, flag:Background) -> unit
    abstract member PutChar : (x:int) * (y:int) * (c:char) * (flag:Background) -> unit
    // ALL BAD ^^^
Run Code Online (Sandbox Code Playgroud)