Back to Csharplang

Standard Unions

proposals/standard-unions.md

latest792 B
Original Source

Standard Unions

Summary

A family of nominal type unions exist in the TBD namespace that can be used across assemblies as the standard way to specify a type union without needing to declare and name one.

csharp
public union Union<T1, T2>(T1, T2);
public union Union<T1, T2, T3>(T1, T2, T3);
public union Union<T1, T2, T3, T4>(T1, T2, T3, T4);
...

Union<int, string> x = 10;
...
var _ = x switch 
{
    int v => ...,
    string v => ...
}

Detailed Design

  • A small number of generic nominal type unions named Union are added to a runtime library.
  • The union are declared using the Nominal Type Union syntax and generated by the C# compiler.

Runtime Library

The union types are added to the same assembly that houses the ValueTuple, Func and Action types.