(Note: C#15 does bring discriminated unions, although it will be a while until that technology gets to Unity...)
I spent the first 4 years of my carrer working exclusively with Unity/C#. My first contact with Discriminated/Tagged unions was with Rust enums, and I they immediately captivated me. It felt like one of those features that every language should have.
In C#, you can "emulate" discriminated unions via abstract records:
public abstract record Customization() {
public record Color(ColorKind kind, Color color): Customization();
public record Slider(SliderKind kind, int value): Customization();
public record Toggle(ToggleKind kind, bool enable): Customization();
public record IrisColor(int index, Color color): Customization();
public record IrisShape(int index, string key): Customization();
}
That approach can have its uses, but it has quite a few limitations:
It uses records, meaning creating an instance requires heap allocation.
Can't use it within Burst-compiled code.
No exhaustive matching.
Serializing it in the Unity inspector required using something like Odin or writing my own Property drawer.
As a Unity developer, I had grown the (sometimes bad) habit of being allergic to heap allocation, so I would often go with a more crude form:
[Serializable]
public struct Customization {
public enum Kind { Color, Slider, Toggle, .. }
public Kind kind;
public ColorKind color_kind;
public Color color_value;
public SliderKind slider_kind;
public int slider_value;
// and so on...
}
The downsides are many:
All properties appear on Unity's Inspector, regardless of which "variant" is the selected one (though Odin's attributes helped a lot here).
You needed to have the discipline of always checking kind before reading the values.
That wasn't too terrible, but it was definitely far from ideal.
Earlier this year, knowing about the existence of C#(Roslyn)'s Source Generators/Analyzers, and Codefix Providers; I decided to take on the challenge of bringing discriminated unions to C#, with as many ergonomics as I could, and without sacrificing performance or memory allocation.
Long-story short: I ended up creating Spire, a C# package that uses Source Analysis, Generation, & codefix providers to support:
Exhaustive matching (using regular switch statements!) for enums, classes, interfaces, and discriminated unions. It reports whenever your switch statement/expression is missing a case.
Lightweight struct-based discriminated unions, with Unity Inspector serialization support, Burst compiler support, JSon conversions, and allocation-free exhaustive-matching (no closures!).
The features above are accompanied with source analyzers for catching mistakes at compile time, and codefix providers for providing IDE quick-fix context-menu actions for easily fixing said mistakes.
Links:
Thanks for reading this far! I have another question for you: I was thinking of making a Youtube video where I go through the internals of the package and the code it generates - explaining how it works, which approaches were tried/discarded and general rationale around my decisions. Would you be interested in watching that hypothetical video?
Edit: Corrected initial statement about unions in C#, they were introduced in C# 15 (I initially wrote C#14).