Skip to content

Instantly share code, notes, and snippets.

@ehamberg
Created October 23, 2013 16:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ehamberg/7121588 to your computer and use it in GitHub Desktop.
Save ehamberg/7121588 to your computer and use it in GitHub Desktop.
template <class T> struct Maybe { virtual ~Maybe() {}; };
template <class T> struct Nothing : public Maybe<T> {};
template <class T> struct Just : public Maybe<T> { Just(T v) : v(v) {} T v; };
template <class T>
T fromMaybe(const Maybe<T>& maybeVal, const T& defaultVal)
{
if (const Just<T>* j = dynamic_cast<const Just<T>*>(&maybeVal)) {
return j->v;
}
return defaultVal;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment