Back to Language Ext

README

LanguageExt.Core/Monads/Alternative Monads/README.md

4.4.41.9 KB
Original Source

Alternative Monads are monadic types that can have an alternative value! What does this mean?

If first we think about a tuple:

(int, string)

This type can represent an int AND a string. Now consider the Either<L, R> monad, this means the value can beLeft OR Right (L or R).

So, this:

Either<int, string>

Means either int OR string. It is the natural dual of tuple.

In the case of Either the Right value is considered the bound value of the monad, and the Left value is considered the alternative value. All the other alternative value monads can be seen as derivatives or specialisations of Either.

TypeAlternative Value TypeBound Value TypeNotes
Either<L, R>LR
Fin<A>ErrorAEquivalent to Either<Error, A>
Try<A>ErrorAEquivalent to Either<Error, A> (that catches exceptions!)
Option<A>NoneAEquivalent to Either<Unit, A>
Nullable<A>nullAEquivalent to Either<Unit, A>
Validation<Fail, Succ>FailSuccFail must be a Monoid<Fail> to collect errors

The alternative value is usually used to carry errors, but that doesn't have to be the case. It is important to remember that the alternative-value can be for any purpose you want.