Reputation: 10781
Related to No argument names in abstract declaration?, how would you do this with a C#-style tupled argument? So if I wanted
abstract member createEmployee : (string * string) -> Employee
how would I be able to communicate whether it should be
1- member this.createEmployee(firstName, lastName) = ...
2- member this.createEmployee(lastName, firstName) = ...
Use case: creating an interface in F# to be used from C#.
Upvotes: 2
Views: 94
Reputation: 10781
abstract createEmployee: firstName:string * lastName:string -> Employee
compiles to create C# invocation semantics and the names appear both in C# and F# intellisense in Visual Studio 2013.
Upvotes: 7
Reputation: 7735
There are two ways to do that:
Use Named Arguments as described in MSDN
member this.createEmployee(firstName: string, lastName: string) = ...
// external code
MyClass.createEmployee(firstName = "John", lastName = "Smith")
MyClass.createEmployee(lastName = "Smith", firstName = "John")
Make your class member accept a structure as an argument:
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"}
The choice is yours; the former allows for optional arguments (consider middleName
or salutation
) while the latter allows for storing the structure together (e.g. if you need to process it further).
Upvotes: 3
Reputation: 52270
I think the easiest way is to go like this:
type FirstName = FirstName of string
type LastName = LastName of string
...
abstract member createEmployee : (FirstName * LastName) -> Employee
you probably want it to be
abstract member createEmployee : FirstName * LastName -> Employee
instead as there are slight differences ;)
Upvotes: 3