F# 活动模式作为非静态成员

发布于 2024-08-12 16:25:29 字数 749 浏览 5 评论 0原文

我不确定是否允许非静态公共成员活动模式,但您可以定义它们而不会引起编译器的抱怨。如果允许的话,匹配的语法是什么?编译器给我 FooBar2.doSomething 中的 Foo 类型不匹配。期待 'a ->给定 'a -> 选择<'b,'c> 'd->选择<单位,单位>

// 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 -> ()

I'm not sure if non-static public member active patterns are allowed but you can define them without the compiler complaining. If they are allowed what's the syntax for matching against one? The compiler is giving me a type mismatch for Foo in FooBar2.doSomething. Expecting a 'a -> Choice<'b,'c> given '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 -> ()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

记忆之渊 2024-08-19 16:25:29

活动模式不应用作成员。事实上,这些编译器完全是一个编译器错误,我们将修复该错误(感谢您的报告:))。使用本地或模块绑定的“let”来定义活动模式。

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.

前事休说 2024-08-19 16:25:29

我对此不起作用并不感到惊讶,而且我看不到例如活动模式的自然语义解释。当您看到 Foo 模式时,如何知道要使用哪个实例?您是否可以为 FooBar 案例提供不同的实例(因此模式匹配不完整)?这里的问题似乎没有一个优雅的解决方案。老实说,我很惊讶即使是静态情况也能工作,而且我在规范中没有看到任何内容将活动模式的定义作为任何类型的成员。

I'm not surprised that this doesn't work, and I can't see a natural semantic interpretation for instance active patterns. How do you know which instance to use when you see the Foo pattern? Could you have different instances for the Foo and Bar cases (and therefore an incomplete pattern match)? There doesn't seem to be an elegant resolution to the issues here. To be honest, I'm suprised that even the static case works, and I don't see anything in the spec addressing the definition of active patterns as members of any kind.

独行侠 2024-08-19 16:25:29

成员识别器似乎从 1.9.9.9 版本开始就不再存在了。即使对于静态成员也是如此。我认为这是一种耻辱,因为它允许识别器过载。我可以有一个用于类型、MemberInfo 等的“名称”识别器。现在我需要有一个“Type_Name”。 'Member_Name' 等以避免命名冲突。只是“名字”更好。

Member recognizers seem to be out since version 1.9.9.9. even for static members. I think it is a shame because it allowed for recognizer overloading. I could have a 'Name' recognizer for Type, MemberInfo etc. Now I need to have a 'Type_Name'. 'Member_Name' etc. to avoid naming conflicts. Just 'Name' was nicer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文