Home >Backend Development >C++ >How to Create Type-Safe Discriminated Unions (Tagged Unions) in C#?

How to Create Type-Safe Discriminated Unions (Tagged Unions) in C#?

Susan Sarandon
Susan SarandonOriginal
2025-01-04 16:11:40642browse

How to Create Type-Safe Discriminated Unions (Tagged Unions) in C#?

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn