In the meanwhile, here's a little class I made that implemented just enough pattern matching as a fluent interface : https://gist.github.com/neuschwanstein/6c0c9256c1dc5b370011.
Not pretty, but it works well enough for a pragmatic programmer.
Not pretty, but it works well enough for a pragmatic programmer.
may.Match(
() => print("None"),
val => print("Some: " + val))
Not very flexible, or performant, but covers the basics of common cases.[1]: http://journal.stuffwithstuff.com/2009/05/13/ml-style-patter...
As well as a generic extension on Object I started adding bespoke Match extension methods to a monads library that I've been working on [1] (which again was all about trying to coerce C# into a more expression based style).
So the Option monad Match method would work like so:
Func<int> res = (from x in DoSomething()
from y in DoSomethingElse()
select x + y)
.Match(
Just: v => v * 10,
Nothing: 0
);
Or, the Either monad: var result =
(from lhs in Two()
from rhs in Two()
select lhs + rhs)
.Match(
Right: r => r * 2,
Left: l => 0
);
[1] https://github.com/louthy/csharp-monad