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
You called your constructor
Corp_SME
but try to match it withCORP_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).