有没有办法在构造函数中命名函数参数?
type UnnamedInCtor(foo: string -> string -> bool) =
member this.Foo: string -> string -> bool = foo
member this.Bar: a:string -> b:string -> bool = foo
member this.Fizz = foo
//Does not compile
type NamedInCtor(foo: a:string -> b:string -> bool) =
member this.Foo: string -> string -> bool = foo
member this.Bar: a:string -> b:string -> bool = foo
member this.Fizz = foo
Run Code Online (Sandbox Code Playgroud)
我认为这在 F# 中是不可能的,但是如果您想记录 foo 代表的内容,可以使用类型缩写:
// Compiles
type aToBToC = string -> string -> bool
type NamedInCtor(foo: aToBToC) =
member this.Foo: string -> string -> bool = foo
member this.Bar: a:string -> b:string -> bool = foo
member this.Fizz = foo
Run Code Online (Sandbox Code Playgroud)