4

非静的なパブリック メンバーのアクティブ パターンが許可されているかどうかはわかりませんが、コンパイラが文句を言うことなくそれらを定義できます。それらが許可されている場合、1 つに一致するための構文は何ですか? コンパイラは、FooBar2.doSomething の Foo の型の不一致を示しています。'a -> Choice<'b,'c>与えられた期待'a -> 'd -> Choice<unit,unit>

// No error in this class, static works great
type FooBar() = 
    static member (|Foo|Bar|) (x, y) =
        match x = y with
        | true -> Foo
        | false -> Bar

    member x.doSomething y =
        match x, y with
        | Foo -> ()
        | Bar -> ()

type FooBar2() = 
    member x.(|Foo|Bar|) y =
        match x = y with
        | true -> Foo
        | false -> Bar

    // compiler error on "Foo"    
    member x.doSomething y =
        match y with
        | Foo -> ()
        | Bar -> ()
4

3 に答える 3

6

Active patterns should not be used as members. The fact that these compile at all is a compiler bug that we'll fix (thanks for the report :) ). Use local or module-bound "let"s to define an active pattern.

于 2009-12-02T17:05:15.667 に答える
3

これがうまくいかなくても不思議ではありません。また、アクティブなパターンの自然な意味解釈が見られません。Fooパターンが表示されたときに、どのインスタンスを使用するかをどのように判断しますか? Fooとケースの異なるインスタンスを使用できますかBar(したがって、不完全なパターン一致)? ここの問題に対するエレガントな解決策はないようです。正直なところ、静的なケースでさえ機能することに驚いています。仕様には、アクティブなパターンの定義を何らかのメンバーとして扱うものは何もありません。

于 2009-12-02T04:43:22.490 に答える
1

メンバー認識機能は、バージョン 1.9.9.9 以降に出ているようです。静的メンバーの場合でも。レコグナイザーのオーバーロードが許されているので、残念だと思います。Type、MemberInfo などの「名前」レコグナイザーを使用できます。次に、「Type_Name」が必要です。名前の競合を避けるために「Member_Name」など。「名前」だけの方が良かったです。

于 2010-02-18T07:02:39.847 に答える