-
-
Save zhengbli/6652e1c6d8e10e271e5083918cf60e1a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Either<TDefault, TOther> : IEquatable<Either<TDefault, TOther>> | |
{ | |
private readonly Union<TDefault, TOther> _union; | |
public Either(TDefault value) | |
{ | |
_union = value; | |
} | |
public Either(TOther value) | |
{ | |
_union = value; | |
} | |
public static implicit operator Either<TDefault, TOther>(TDefault value) | |
{ | |
return new Either<TDefault, TOther>(value); | |
} | |
public static implicit operator Either<TDefault, TOther>(TOther value) | |
{ | |
return new Either<TDefault, TOther>(value); | |
} | |
public Either<TResult, TOther> Map<TResult>(Func<TDefault, TResult> map) | |
{ | |
return _union.Match( | |
x => new Either<TResult, TOther>(map(x)), | |
other => new Either<TResult, TOther>(other) | |
); | |
} | |
public Either<TResult, TOther> FlatMap<TResult>(this Either<TDefault, TOther> input, Func<TDefault, Either<TResult, TOther>> map) | |
{ | |
return _union.Match( | |
x => new Either<TResult, TOther>(map(x)), | |
other => new Either<TResult, TOther>(other) | |
); | |
} | |
... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment