4 ms·
I know this doesn't solve your problem in C#, but F# gives you a compiler warning in such a case. The following code compiles without warnings or errors: typ
by khaledh 9y ago
I know this doesn't solve your problem in C#, but F# gives you a compiler warning in such a case. The following code compiles without warnings or errors:
type Color = Red | Green | Blue
let superheroFromColor c =
match c with
| Red -> "Superman"
| Green -> "Green Lantern"
| Blue -> "Spiderman"
However, if you add Yellow as a new case to the Color discriminated union without adding a corresponding match pattern everywhere you match a Color value, you get a compiler warning:
type Color = Red | Green | Blue | Yellow
...
match c with
----------^
warning FS0025: Incomplete pattern matches on this expression. For example, the value 'Yellow' may indicate a case not covered by the pattern(s).
Aside: notice how terse and readable the code is compared to the C# equivalent; F# tends to have a high signal-to-noise ratio.