Skip to content

Instantly share code, notes, and snippets.

@einarwh
Last active Oct 11, 2017
Embed
What would you like to do?
public abstract class Mayhaps<T>
{
private Mayhaps() { }
public abstract bool HasValue { get; }
public abstract T Value { get; }
private class MayhapsIndeed : Mayhaps<T>
{
private readonly T _value;
public MayhapsIndeed(T value)
{
_value = value;
}
public override bool HasValue => true;
public override T Value => _value;
}
private class MayhapsSorry : Mayhaps<T>
{
public override bool HasValue => false;
public override T Value
{
get { throw new InvalidOperationException("Nothing here."); }
}
}
private static MayhapsSorry _sorry = new MayhapsSorry();
public static Mayhaps<T> Indeed(T value) =>
new MayhapsIndeed(value);
public static Mayhaps<T> Sorry => _sorry;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment