You won't be able to leverage C#'s pattern-matching effectively with a library.
Really though, you don't need a library to do sum-types in C#:
It's best to just use records for now:
public abstract record Maybe<A>
{
private Maybe() { }
public sealed record Just(A Value) : Maybe<A>;
public sealed record Nothing : Maybe<A>;
}
The private constructor and sealed case-types stops anybody else deriving from `Maybe<A>`, so the type is effectively closed and finite.
You can then construct like so:
var mx = new Maybe<int>.Just(123);
var my = new Maybe<int>.Nothing();
Then you can use the pattern-matching:
var r = mx switch
{
Maybe<int>.Just (var x) => x,
Maybe<int>.Nothing => 0
};
Of course, we don't get exhaustiveness checking, that'll have to wait for the proper sum-types (although the compiler does some pretty good work on this right now); but until then this is the most expressive and powerful way of doing sum-types in C#.
And, if you want a load of pre-built ones, then my library language-ext will help [1]
Really though, you don't need a library to do sum-types in C#:
It's best to just use records for now:
The private constructor and sealed case-types stops anybody else deriving from `Maybe<A>`, so the type is effectively closed and finite.You can then construct like so:
Then you can use the pattern-matching: Of course, we don't get exhaustiveness checking, that'll have to wait for the proper sum-types (although the compiler does some pretty good work on this right now); but until then this is the most expressive and powerful way of doing sum-types in C#.And, if you want a load of pre-built ones, then my library language-ext will help [1]
[1] https://github.com/louthy/language-ext/