有没有办法为F#构造函数中的函数指定命名参数?

Ken*_*Ito 5 f#

有没有办法在构造函数中命名函数参数?

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)

Tom*_*ski 1

我认为这在 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)