From 894834b5d5fe1621271ca11439e2c753d3b25724 Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Wed, 20 Oct 2021 19:16:01 +0100 Subject: LibJS: Allow construction of ThrowCompletionOr from non-Value TL;DR: Instead of this: return { TRY(my_object()) }; we can now do: return TRY(my_object()); just like we mostly did for Value return types before ThrowCompletionOr. --- Userland/Libraries/LibJS/Runtime/Completion.h | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'Userland') diff --git a/Userland/Libraries/LibJS/Runtime/Completion.h b/Userland/Libraries/LibJS/Runtime/Completion.h index 9874d42f8e..1486aba275 100644 --- a/Userland/Libraries/LibJS/Runtime/Completion.h +++ b/Userland/Libraries/LibJS/Runtime/Completion.h @@ -134,6 +134,15 @@ public: VERIFY(!value.is_empty()); } + // Allows implicit construction of ThrowCompletionOr from a type U if T(U) is a supported constructor. + // Most commonly: Value from Object* or similar, so we can omit the curly braces from "return { TRY(...) };". + // Disabled for POD types to avoid weird conversion shenanigans. + template + ThrowCompletionOr(WrappedValueType value) requires(!IsPOD) + : m_value(move(value)) + { + } + [[nodiscard]] bool is_throw_completion() const { return m_throw_completion.has_value(); } Completion const& throw_completion() const { return *m_throw_completion; } -- cgit v1.2.3