Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
Pattern matching enums: adjust warning #652
Pattern matching enums: adjust warning
Enums are important for interop with non-F# .Net code. Currently, pattern matching an Enum gives a warning:
type E = | A = 0 let f = function // "FS0025 Incomplete pattern matches on this expression". | E.A -> "A"
I suggest that a different warning be given where:
In this case the warning should be "FS**** Enums may take values outside of known cases."
To avoid seeing the FS0025 warning, a user must reduce compile-time safety:
let g : E -> unit = function | _ -> "A"
let h = function | E.A -> "A" | _ -> failwith "I didn't want to see a warning so I am failing at runtime."
Pros of change
The change will distinguish between incompleteness resulting from the user failing to match all known enums, which is highly likely to lead to runtime failure, and incompleteness resulting from invalid enums, which is much less likely and indicates a problem with other code, not the code that gives the warning.
With the change, to avoid seeing the warning, a user may simply disable the FS**** warning. This will preserve the safety of exhaustive pattern matching. The user will be warned about additions to the enum for example with an FS0025 warning.
This is with the proviso that you are relying on other code not to be using invalid Enums, and that you have the correct enumeration of the enum at compile time. Both of which are reasonable assumptions.
Cons of change
A small amount of extra code would be needed in the pattern match exhaustiveness check.
There are people who think there should be a warning. This is why this suggestion is to change the warning rather than eliminate it (which would have been my personal preference).
Estimated cost (XS, S, M, L, XL, XXL):
Affidavit (please submit!)
Please tick this by placing a cross in the box:
Please tick all that apply: