gra*_*bot 4 f# member active-pattern
我不确定是否允许非静态公共成员活动模式,但您可以在没有编译器抱怨的情况下定义它们.如果允许它们与一个匹配的语法是什么?编译器在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 -> ()
Run Code Online (Sandbox Code Playgroud)
不应将活动模式用作成员.事实上,这些编译完全是我们将修复的编译器错误(感谢报告:)).使用本地或模块绑定的"let"来定义活动模式.