这是F#中抽象成员的典型声明:
abstract member createEmployee : string -> string -> Employee
您可以定义参数类型,但不能定义它们的名称。没有名称,在实现接口时如何判断每个参数是什么?换句话说,您如何知道界面是否希望实现为1-或2 - ?
1- member this.createEmployee firstName lastName = ...
2- member this.createEmployee lastName firstName = ...
我是从错误的角度看待问题(习惯于C#)?
答案 0 :(得分:37)
怎么样:
abstract member createEmployee : firstName:string -> lastName:string -> Employee
答案 1 :(得分:6)
这种语法非常适合IMO。我想用参数作为元组(比如C#方法)来做这个,并且只有通过反复试验我才发现这个有效:
abstract member PutChar : x:int * y:int * c:char * flag:Background -> unit
这个丑陋的变体也有效:
abstract member PutChar : x : int * y : int * c : char * flag : Background -> unit
以下是所有感觉合理但却因同样错误而失败的事情 - 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 ^^^