From 666c323577ba9b659c7bc70410831485e16490da Mon Sep 17 00:00:00 2001 From: Manuel Zahariev Date: Mon, 10 Mar 2025 14:41:43 -0700 Subject: [PATCH] AK/Error: Add value_or convenience method - In the style of Optional::value_or - Only const& flavor - WARNING: no unit test (no unit tests for Error), but seems benign --- AK/Error.h | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/AK/Error.h b/AK/Error.h index 910f917bd40..b993747ff05 100644 --- a/AK/Error.h +++ b/AK/Error.h @@ -151,6 +151,12 @@ public: T& value() { return m_value_or_error.template get(); } T const& value() const { return m_value_or_error.template get(); } + [[nodiscard]] ALWAYS_INLINE T const& value_or(T const& fallback) const + { + if (is_error()) + return fallback; + return m_value_or_error.template get(); + } ErrorType& error() { return m_value_or_error.template get(); } ErrorType const& error() const { return m_value_or_error.template get(); }