Discriminated Union Match

Using F # for the first time for a production thing and need a little help. See this code in which I added warnings that I receive 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 does not bluff, the third and other cases are really ignored. What I won’t get here? (Pd and the sales data that I use in the actual implementation, I just left the formulas here.)

What I want to do is use a discriminatory union, as I would use an enumeration in C # and then include it. So in C #, I would type 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; 
        }
    }

Can anyone help me out?

Thanks in advance,

Geert Yang

+3
source share
2 answers

Corp_SME, Corp_SME ( ). - , F# ( , ), , , ( , ).

+6

enum F #. :

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

enum , , - | AssetClass.Corp -> ...

, [<RequireQualifiedAccess>]. , , ( , DU ).

+2

Source: https://habr.com/ru/post/1793571/


All Articles