Skip to content

Instantly share code, notes, and snippets.

@zhengbli
Last active December 8, 2019 04:34
Show Gist options
  • Save zhengbli/6652e1c6d8e10e271e5083918cf60e1a to your computer and use it in GitHub Desktop.
Save zhengbli/6652e1c6d8e10e271e5083918cf60e1a to your computer and use it in GitHub Desktop.
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