.NET (OK, C#) finally gets union types

20 points by ubernostrum


mattgreenrocks

Excited for this! Case inexhaustiveness only being a warning feels like a bit of a footgun though.

typesanitizer

Responses to some questions that compiler/PL people might have (disclaimer: not a C# expert, just skimmed https://github.com/dotnet/csharplang/blob/main/meetings%2Fworking-groups%2Fdiscriminated-unions%2Funion-proposals-overview.md + proposals linked from there)

  1. Like type-theoretic unions, this will do merging if you instantiate a generic union with the same arguments.
    • However, direct initialization of such a type (maybe? not sure?) will go through an overload resolution error.
    • Switching cannot distinguish between cases because there are no separate tags.
  2. Unlike type-theoretic unions, type equality seems to be nominal, not structural. (Could not find definitive wording about this -- inferring from examples)
  3. I found this proposal for adding sugar for tags. https://github.com/dotnet/csharplang/blob/main/proposals%2Fcase-declarations.md
    • AFAICT, this doesn't naturally allow for layout optimizations
  4. I don't think there's anything preventing you from performing side effects in the functions related to pattern matching (so reordering would require purity analysis).
  5. I couldn't find anything related to exhaustiveness checking when it comes to arbitrary pattern nesting.