From 3ca86cd0448ad75ebf24be1ddf87af4b1396b0d3 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Thu, 1 Feb 2024 12:41:46 -0500 Subject: [PATCH] LibJS: Allow creating the specialized Optional from OptionalNone This allows, for example: ThrowCompletionOr> foo() { return OptionalNone {}; } The constructors and constraints here are lifted verbatim from AK::Optional. --- Userland/Libraries/LibJS/Runtime/Value.h | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/Userland/Libraries/LibJS/Runtime/Value.h b/Userland/Libraries/LibJS/Runtime/Value.h index d63f53357a..68b38978e4 100644 --- a/Userland/Libraries/LibJS/Runtime/Value.h +++ b/Userland/Libraries/LibJS/Runtime/Value.h @@ -602,6 +602,9 @@ public: Optional() = default; + template V> + Optional(V) { } + Optional(Optional const& other) { if (other.has_value()) @@ -614,12 +617,20 @@ public: } template + requires(!IsSame>) explicit(!IsConvertible) Optional(U&& value) requires(!IsSame, Optional> && IsConstructible) : m_value(forward(value)) { } + template V> + Optional& operator=(V) + { + clear(); + return *this; + } + Optional& operator=(Optional const& other) { if (this != &other) {