2016-03-22 14 views
4

コンストラクタで関数の引数に名前を付ける方法はありますか?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 

答えて

1

私はあなたがfooが何を表すか文書化したい場合しかし、あなたはtype abbreviationsを使用することができ、それはF#では不可能だと思う:

// 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 
1

あなたのコンストラクタで解除カレー機能する必要があります:

type NamedInCtor(a, b) = 
    member this.Foo: string -> string -> bool = a b 
    member this.Bar: string -> string -> bool = a b 
    member this.Fizz = a b 

注aとbが暗黙のうちにここに入力されていること。コードをもっと読みやすくするため、できるだけコンパイラを信頼する必要があります。

関数はファーストクラスの型であり、伝統的なオブジェクトは推奨されません。あなたが求めているのは、本質的に「このタイプの任意のサブセットに名前を付けてアクセスできますか?それに対する答えは「いいえ」です。その振る舞いが必要な場合は、関数を構造化して要求する必要があります。

+1

あなたがこれを確認しました。コンパイルされません。 –

+0

もちろん私はしませんでした;) –

関連する問題