Explorar o código

LibJS: Allow creating the specialized Optional<Value> from OptionalNone

This allows, for example:

    ThrowCompletionOr<Optional<Value>> foo()
    {
        return OptionalNone {};
    }

The constructors and constraints here are lifted verbatim from
AK::Optional.
Timothy Flynn hai 1 ano
pai
achega
3ca86cd044
Modificáronse 1 ficheiros con 11 adicións e 0 borrados
  1. 11 0
      Userland/Libraries/LibJS/Runtime/Value.h

+ 11 - 0
Userland/Libraries/LibJS/Runtime/Value.h

@@ -602,6 +602,9 @@ public:
 
     Optional() = default;
 
+    template<SameAs<OptionalNone> V>
+    Optional(V) { }
+
     Optional(Optional<JS::Value> const& other)
     {
         if (other.has_value())
@@ -614,12 +617,20 @@ public:
     }
 
     template<typename U = JS::Value>
+    requires(!IsSame<OptionalNone, RemoveCVReference<U>>)
     explicit(!IsConvertible<U&&, JS::Value>) Optional(U&& value)
     requires(!IsSame<RemoveCVReference<U>, Optional<JS::Value>> && IsConstructible<JS::Value, U &&>)
         : m_value(forward<U>(value))
     {
     }
 
+    template<SameAs<OptionalNone> V>
+    Optional& operator=(V)
+    {
+        clear();
+        return *this;
+    }
+
     Optional& operator=(Optional const& other)
     {
         if (this != &other) {