抽象元组函数中的参数名称

Parameter names in abstract tupled function

No argument names in abstract declaration? 相关,您将如何使用 C# 样式的元组参数执行此操作?所以如果我想要

abstract member createEmployee : (string * string) -> Employee

我如何才能传达它是否应该

1-   member this.createEmployee(firstName, lastName) = ...
2-   member this.createEmployee(lastName, firstName) = ...

用例:在 F# 中创建要从 C# 使用的接口。

我认为最简单的方法是这样:

type FirstName = FirstName of string
type LastName  = LastName of string

...

abstract member createEmployee : (FirstName * LastName) -> Employee

备注:

你可能希望它是

abstract member createEmployee : FirstName * LastName -> Employee

相反,因为存在细微差别 ;)

有两种方法可以做到这一点:

  1. 使用 MSDN

    中所述的命名参数
    member this.createEmployee(firstName: string, lastName: string) = ...
    
    // external code
    MyClass.createEmployee(firstName = "John", lastName = "Smith")
    MyClass.createEmployee(lastName = "Smith", firstName = "John")
    
  2. 让您的 class 成员接受结构作为参数:

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

选择权在你;前者允许可选参数(考虑 middleNamesalutation),而后者允许将结构存储在一起(例如,如果您需要进一步处理它)。

abstract createEmployee: firstName:string * lastName:string -> Employee

编译以创建 C# 调用语义,名称出现在 Visual Studio 2013 年的 C# 和 F# intellisense 中。