Discriminating Union in C#
Question:
How to create a discriminating union in C# (also called tagged union)? This union allows different types to be used to represent a single value and provides compile-time type safety.
Answer:
Using generic type parameter constraints, you can create type-safe discerning unions. The following code demonstrates how to create a discriminating union with three cases (int, char, and string):
public abstract class Union3<A, B, C> { public abstract T Match<T>(Func<A, T> f, Func<B, T> g, Func<C, T> h); private Union3() { } public sealed class Case1 : Union3<A, B, C> { public readonly A Item; public Case1(A item) : base() { this.Item = item; } public override T Match<T>(Func<A, T> f, Func<B, T> g, Func<C, T> h) { return f(Item); } } public sealed class Case2 : Union3<A, B, C> { public readonly B Item; public Case2(B item) { this.Item = item; } public override T Match<T>(Func<A, T> f, Func<B, T> g, Func<C, T> h) { return g(Item); } } public sealed class Case3 : Union3<A, B, C> { public readonly C Item; public Case3(C item) { this.Item = item; } public override T Match<T>(Func<A, T> f, Func<B, T> g, Func<C, T> h) { return h(Item); } } }
To create an instance of the discriminating union, use a static factory method to specify the corresponding case and provide value. For example:
Union3<int, char, string> union1 = Union3<int, char, string>.Case1(5); Union3<int, char, string> union2 = Union3<int, char, string>.Case2('x'); Union3<int, char, string> union3 = Union3<int, char, string>.Case3("Juliet");
By using the Match method, you can safely access the value of the union on a specific case basis. For example:
string value1 = union1.Match(num => num.ToString(), character => new string(new char[] { character }), word => word); string value2 = union2.Match(num => num.ToString(), character => new string(new char[] { character }), word => word); string value3 = union3.Match(num => num.ToString(), character => new string(new char[] { character }), word => word);
This method uses compile-time type checking to ensure that the correct function is provided for the given case. The compiler will throw an error if you try to access a value that does not match the case.
The above is the detailed content of How to Create Type-Safe Discriminated Unions (Tagged Unions) in C#?. For more information, please follow other related articles on the PHP Chinese website!