匹配受歧视的工会

发布于 2024-10-18 06:58:46 字数 1074 浏览 8 评论 0原文

第一次使用 F# 进行生产,需要一些帮助。请参阅这段代码,我在每行上添加了作为注释收到的警告:

type AssetClass = 
    | Corp
    | Corp_SME
    | Res_Mort
    | Qual_Ret
    | Ret_Oth

let Correlation assetClass pd sales = 
    match assetClass with 
    | Corp -> 0.12 
    | CORP_SME -> 0.24 // warning FS0049: Uppercase variable identifiers
    | Res_Mort -> 0.15 // warning FS0026: This rule will never be matched
    | Qual_Ret -> 0.04 // warning FS0026: This rule will never be matched
    | Ret_Oth  -> 0.03 // warning FS0026: This rule will never be matched

我检查过,这不是虚张声势,第三种情况和其他情况确实被忽略了。我在这里没有得到什么? (我在实际实现中确实使用了 pd 和 sales 输入,我只是在这里省略了公式。)

我想要做的是使用可区分联合,就像我在 C# 中使用枚举一样,然后打开它。所以在 C# 中我会输入这样的内容:

    enum AssetClass {
        Corp,
        Corp_SME,
        Ret_Oth
    }

    float Correlation(AssetClass assetClass){
        switch(assetClass){
            case Corp: return 0.12; 
            case Corp_SME: return 0.12;
            case Ret_Oth: return 0.12; 
        }
    }

有人能帮我吗?

提前致谢,

格特·扬

Using F# for the first time for a production thing and need a little help. Please see this code where I added the warnings I get as comments on each line:

type AssetClass = 
    | Corp
    | Corp_SME
    | Res_Mort
    | Qual_Ret
    | Ret_Oth

let Correlation assetClass pd sales = 
    match assetClass with 
    | Corp -> 0.12 
    | CORP_SME -> 0.24 // warning FS0049: Uppercase variable identifiers
    | Res_Mort -> 0.15 // warning FS0026: This rule will never be matched
    | Qual_Ret -> 0.04 // warning FS0026: This rule will never be matched
    | Ret_Oth  -> 0.03 // warning FS0026: This rule will never be matched

I checked and it's not bluffing, the third and other cases really are ignored. What am I not getting here? (The pd and sales inputs I do use in the real implementation, I just left out the formulas here.)

What I want to do is use the discriminated union as I would use an enum in C#, and then switch on it. So in C# I would have typed this:

    enum AssetClass {
        Corp,
        Corp_SME,
        Ret_Oth
    }

    float Correlation(AssetClass assetClass){
        switch(assetClass){
            case Corp: return 0.12; 
            case Corp_SME: return 0.12;
            case Ret_Oth: return 0.12; 
        }
    }

Could someone help me out?

Thanks in advance,

Gert-Jan

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

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

发布评论

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

评论(2

陈独秀 2024-10-25 06:58:46

您调用了构造函数 Corp_SME,但尝试将其与 CORP_SME(全部大写)匹配。由于这不是任何构造函数的名称,F# 假定它是一个变量名称(因此出现有关大写变量名称的警告),它当然会匹配之前未匹配的所有内容(因此出现后续警告)。

You called your constructor Corp_SME but try to match it with CORP_SME (all caps). Since this is not the name of any constructor, F# assumes it's a variable name (thus the warning about upper case variable names), which then of course matches everything not previously matched (thus the subsequent warnings).

谁把谁当真 2024-10-25 06:58:46

顺便说一句,您还可以在 F# 中声明 enum 类型。为此,您只需向类型的情况添加一些整数值:

type AssetClass = 
    | Corp = 0
    | Corp_SME = 1
    | Res_Mort = 2
    | Qual_Ret = 3 
    | Ret_Oth = 4

要在模式匹配中使用enum,您必须使用完全限定名称,这样您就不会意外地遇到问题- 你必须写 | AssetClass.Corp -> ...

如果您使用 [] 属性注释类型,您也可以为通常的可区分联合获得此行为。这可能是一个好主意,因为您不会污染名称空间(但我仅在有太多 DU 或一些冲突名称时才使用它)。

As a side-note, you can also declare enum types in F#. To do that, you just add some integer values to the cases of the type:

type AssetClass = 
    | Corp = 0
    | Corp_SME = 1
    | Res_Mort = 2
    | Qual_Ret = 3 
    | Ret_Oth = 4

To use the enum in pattern matching, you then have to use fully qualified name, so you cannot accidentally get the issue you got - you have to write | AssetClass.Corp -> ...

You can also get this behavior for usual discriminated unions if you annotate the type with [<RequireQualifiedAccess>] attribute. This is probably a good idea as you're not polluting the namespace (but I use it only when I have too many DUs or some conflicting names).

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