Skip to content

Instantly share code, notes, and snippets.

@einarwh
Last active October 11, 2017 20:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save einarwh/0df548e1496d561242ab659d2b3841af to your computer and use it in GitHub Desktop.
Save einarwh/0df548e1496d561242ab659d2b3841af to your computer and use it in GitHub Desktop.
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