Skip to content

Instantly share code, notes, and snippets.

@zhengbli
Last active December 8, 2019 04:33
Show Gist options
  • Save zhengbli/2a5a2add99028e78ee59720cca5c0e05 to your computer and use it in GitHub Desktop.
Save zhengbli/2a5a2add99028e78ee59720cca5c0e05 to your computer and use it in GitHub Desktop.
public abstract class Union<T1, T2> : IEquatable<Union<T1, T2>>
{
public abstract TResult Match<TResult>(Func<T1, TResult> f1, Func<T2, TResult> f2);
public abstract void Match(Action<T1> a1, Action<T2> a2);
private Union() { }
public static implicit operator Union<T1, T2>(T1 item)
{
return new Case1(item);
}
public static implicit operator Union<T1, T2>(T2 item)
{
return new Case2(item);
}
public sealed class Case1 : Union<T1, T2>
{
public T1 Item { get; }
public Case1(T1 item)
{
Item = item;
}
public override TResult Match<TResult>(Func<T1, TResult> f1, Func<T2, TResult> f2)
{
return f1(Item);
}
public override void Match(Action<T1> a1, Action<T2> a2)
{
a1(Item);
}
}
public sealed class Case2 : Union<T1, T2>
{
public T2 Item { get; }
public Case2(T2 item)
{
Item = item;
}
public override TResult Match<TResult>(Func<T1, TResult> f1, Func<T2, TResult> f2)
{
return f2(Item);
}
public override void Match(Action<T1> a1, Action<T2> a2)
{
a2(Item);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment