@@ -175,6 +175,25 @@ class Result<T, Errs...> {
175175 }, std::forward<Self>(self).error );
176176 }
177177
178+ /* *
179+ * @brief If the Result contains an error, return the given value, otherwise return the value of
180+ * the Result
181+ *
182+ * @tparam Self deduced self type
183+ * @tparam U default value type. Must be convertible to Result value type
184+ * @param self the current Result instance
185+ * @param val the value to use if self contains an error
186+ */
187+ template <typename Self, typename U>
188+ requires std::convertible_to<U, T>
189+ constexpr T or_default (this Self&& self, U&& val) {
190+ if (self.has_error ()) {
191+ return std::forward<U>(val);
192+ } else {
193+ return std::forward<Self>(self);
194+ }
195+ }
196+
178197 /* *
179198 * @brief invoke a callable with the error, if present
180199 *
@@ -198,7 +217,8 @@ class Result<T, Errs...> {
198217 // otherwise, invoke the callable
199218 std::visit ([&f](auto && arg) {
200219 // even though this condition is impossible, it's necessary. Otherwise the
201- // compiler will compile a branch where f is invoked with an unsupported argument type
220+ // compiler will compile a branch where f is invoked with an unsupported argument
221+ // type
202222 if constexpr (std::same_as<std::monostate, std::remove_cvref_t <decltype (arg)>>
203223 || !traits::Invocable<Self, F, std::remove_cvref_t <decltype (arg)>>) {
204224 throw std::logic_error (" This exception is unreachable" );
@@ -302,9 +322,9 @@ class Result<void, Errs...> {
302322 /* *
303323 * @brief and_then monadic function
304324 *
305- * The callable must be able to take the perfectly-forwarded value of this Result instance as an
306- * argument. The callable must be able to return any error passed to it. The callable must
307- * return a Result.
325+ * The callable must be able to take the perfectly-forwarded value of this Result instance
326+ * as an argument. The callable must be able to return any error passed to it. The callable
327+ * must return a Result.
308328 *
309329 * @tparam Self deduced self type
310330 * @tparam F the type of the callable
@@ -319,8 +339,9 @@ class Result<void, Errs...> {
319339 // if there is an error, return said error immediately
320340 if (self.has_error ()) {
321341 return std::visit ([](auto && arg) -> std::invoke_result_t <F> {
322- // even though this conditional is impossible, it's necessary to stop the compiler
323- // from thinking that ReturnType could be constructed with std::monostate
342+ // even though this conditional is impossible, it's necessary to stop the
343+ // compiler from thinking that ReturnType could be constructed with
344+ // std::monostate
324345 if constexpr (std::same_as<std::monostate, std::remove_cvref_t <decltype (arg)>>) {
325346 throw std::logic_error (" This exception is unreachable" );
326347 } else {
@@ -401,7 +422,8 @@ class Result<void, Errs...> {
401422 // otherwise, invoke the callable
402423 std::visit ([&f](auto && arg) {
403424 // even though this condition is impossible, it's necessary. Otherwise the
404- // compiler will compile a branch where f is invoked with an unsupported argument type
425+ // compiler will compile a branch where f is invoked with an unsupported argument
426+ // type
405427 if constexpr (std::same_as<std::monostate, std::remove_cvref_t <decltype (arg)>>
406428 || !traits::Invocable<Self, F, std::remove_cvref_t <decltype (arg)>>) {
407429 throw std::logic_error (" This exception is unreachable" );
0 commit comments