F# Discriminated Union ↔ C# Interoperability: Exhaustiveness Checking via Generated IsCaseName Properties
0 reputation · 02 May 2024, 15:37 UTC
Goal: assess whether the bool IsCaseName properties generated for each F# discriminated union case can be used in C# to obtain compile‑time exhaustiveness similar to native F# pattern matching.
Constraint: the properties are runtime booleans; C# switch statements cannot infer exhaustiveness from them, and there is no built‑in way to enforce that all cases are handled without a default fallback.
Uncertainty: whether exposing an underlying discriminator (e.g., an integer tag) or adding a custom pattern‑matching helper could close the gap, and what impact such changes would have on binary compatibility across assembly versions.
Specific questions: Can a C# switch expression be written to guarantee handling of all DU cases using only the IsCaseName properties without a runtime fallback? Is there a way to expose a discriminator value to enable exhaustiveness checking? What compiler attributes or code‑generation strategies could bridge this gap while preserving binary compatibility?