Either Monad in C#— A Functional approach to error handling

if we have a simple try catch block like this :

we could extract the outline of the try catch and inject the two segments of try… and catch, with delegates [this is a functional flavored version of the template method design pattern ].

so we can rewrite it like bellow, in a more general way.

and use it like that :

now we could combine the two execution paths with an Either, that will have either, on the left by convention the exception, if there was one, or on the Right the result if there was no exception .

Either is a common type in functional Languages. It corresponds to the logical Or or + as an algebraic data type . Is commonly called a discriminated union. Which means an Either type can contain any of the types it sums.

in order to find out whats inside either we pattern match it, as we say :

In c# there is this new feature for pattern matching:

which is just a variable assignment along with the standard is — type check.

But in Object oriented languages we always had a discriminating union and its called Inheritance.

How we know what type is the shape ? well because we have the Area() polymorphic method inside shape class, its possible to discriminate them. without if’s.

polymorphism serves as the pattern matching mechanism in OOP

Ok, Lets move to how we could use it in error handling. In Haskell documentation of Either, we read :

The Either type is sometimes used to represent a value which is either correct or an error; by convention, the Left constructor is used to hold an error value and the Right constructor is used to hold a correct value (mnemonic: "right" also means "correct").

i will add a Select method in order to make it a Functor. This select by convention says if there is a result then lift it with Func<T,T1> to T1 and create a new Try <T1> with the new result on the right side.

Now we can also write a Bind (or SelectMany by Linq Convention ) method in order to be able to combine Two of this Try functors and thus make it a Monad.

now we can write something like below. Obviously this is far from functional but its a realistic mix of oop and functional programming.

We could also hide the Result by adding a Cata (from catamorphism ) aka Fold aka Reduce method that usually means that it collapse the structure and extracts the result.

also we can return a Maybe type as a result :

this is like combining two try catch blocks that have in total 4 different execution paths. But we simplified it because we created a generic way to combine successful paths and failed paths using Either and the Monadic Binding.

A totally custom Either implementation can be seen below . The .netfiddle is here

Excerpt of : “ Functional Programming in C#

More than 14 years’ experience as full stack developer and Software Architect .https://leanpub.com/u/dimitrispapadim