Add Optional::map.

This commit is contained in:
John McCall 2019-12-16 11:57:03 -05:00
parent a9db0d9f17
commit 2e2d142efe
1 changed files with 16 additions and 0 deletions

View File

@ -267,6 +267,14 @@ public:
return hasValue() ? getValue() : std::forward<U>(value);
}
/// Apply a function to the value if present; otherwise return None.
template <class Function>
auto map(const Function &F) const
-> Optional<decltype(F(getValue()))> {
if (*this) return F(getValue());
return None;
}
#if LLVM_HAS_RVALUE_REFERENCE_THIS
T &&getValue() && { return std::move(Storage.getValue()); }
T &&operator*() && { return std::move(Storage.getValue()); }
@ -275,6 +283,14 @@ public:
T getValueOr(U &&value) && {
return hasValue() ? std::move(getValue()) : std::forward<U>(value);
}
/// Apply a function to the value if present; otherwise return None.
template <class Function>
auto map(const Function &F) &&
-> Optional<decltype(F(std::move(*this).getValue()))> {
if (*this) return F(std::move(*this).getValue());
return None;
}
#endif
};