与No argument names in abstract declaration?相关,您如何使用C#样式的tupled参数执行此操作?所以,如果我想要
abstract member createEmployee : (string * string) -> Employee
我怎么能沟通是否应该
1- member this.createEmployee(firstName, lastName) = ...
2- member this.createEmployee(lastName, firstName) = ...
使用案例:在F#中创建一个用于从C#中使用的接口。
答案 0 :(得分:6)
abstract createEmployee: firstName:string * lastName:string -> Employee
编译以创建C#调用语义,并且在Visual Studio 2013中,名称同时出现在C#和F#intellisense中。
答案 1 :(得分:3)
我认为最简单的方法就是这样:
type FirstName = FirstName of string
type LastName = LastName of string
...
abstract member createEmployee : (FirstName * LastName) -> Employee
你可能希望它是
abstract member createEmployee : FirstName * LastName -> Employee
因为存在细微差别;)
答案 2 :(得分:3)
有两种方法可以做到:
使用MSDN
中所述的命名参数member this.createEmployee(firstName: string, lastName: string) = ...
// external code
MyClass.createEmployee(firstName = "John", lastName = "Smith")
MyClass.createEmployee(lastName = "Smith", firstName = "John")
让您的类成员接受一个结构作为参数:
type EmployeeName =
struct
val firstName: string
val lastName: string
end
member this.createEmployee2(employeeName: EmployeeName) = ...
// external code
MyClass.createEmployee2 {firstName = "John"; lastName = "Smith"}
MyClass.createEmployee2 {lastName = "Smith"; firstName = "John"}
选择权归你的;前者允许使用可选参数(考虑middleName
或salutation
),而后者允许将结构存储在一起(例如,如果您需要进一步处理它)。