F#活动模式为非静态成员 [英] F# active pattern as non-static member

查看:67
本文介绍了F#活动模式为非静态成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定是否允许使用非静态公共成员的活动模式,但是您可以在没有编译器抱怨的情况下进行定义.如果允许,则匹配一个的语法是什么?编译器在FooBar2.doSomething中为我提供了Foo的类型不匹配的信息.期望给定'a -> 'd -> Choice<unit,unit>

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

推荐答案

不应将活动模式用作成员.这些都可以编译的事实是我们将解决的编译器错误(感谢报告:)).使用本地或模块绑定的"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.

这篇关于F#活动模式为非静态成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆