You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
First up let me say how much I love this project! It's embarrassingly better than my own feeble attempt to wrap C++ types for use with QuickJS, and I've learned several useful template meta-programming tricks from reading the source code.
I hit a snag when I tried to implement a C++ object property that could be set (in JS) to a string, or an integer, or null. Obviously I chose to implement this property as having type std::variant<std::string, int> on the C++ side. But how to represent null? I tried using void*, shared_ptr, optional... but couldn't get anything to work. RTFM'ing it seems std::monostate is the thing to use, i.e. my property type becomes std::variant<std::string, int, std::monostate>.
To get this to work I had to implement a js_traits that can wrap and unwrap std::monostate and also alter the existing js_traits<std::variant<...>>::unwrap() to use std::monostate when given a JS_NULL.
My question is: Have I done the right thing? Is this the best way to implement support for nullable property values?
The text was updated successfully, but these errors were encountered:
I think std::monostate is more similar to js undefined (because js variables are undefined when not initialized). For js nullstd::nullptr_t might be good choice.
For variant support, yes, you currently need to modify quickjspp.hpp to add new types
Just a quick comment to mention that std::variant default-constructs to its first type's default value.
Thus I think typically, std::monostate (or std::nullptr_t) should be your first rather than last type.
And @ftk , very good point about undefined ~= std::monostate and null ~= std::nullptr_t.
First up let me say how much I love this project! It's embarrassingly better than my own feeble attempt to wrap C++ types for use with QuickJS, and I've learned several useful template meta-programming tricks from reading the source code.
I hit a snag when I tried to implement a C++ object property that could be set (in JS) to a string, or an integer, or null. Obviously I chose to implement this property as having type
std::variant<std::string, int>
on the C++ side. But how to represent null? I tried using void*, shared_ptr, optional... but couldn't get anything to work. RTFM'ing it seemsstd::monostate
is the thing to use, i.e. my property type becomesstd::variant<std::string, int, std::monostate>
.To get this to work I had to implement a
js_traits
that can wrap and unwrapstd::monostate
and also alter the existingjs_traits<std::variant<...>>::unwrap()
to use std::monostate when given a JS_NULL.My question is: Have I done the right thing? Is this the best way to implement support for nullable property values?
The text was updated successfully, but these errors were encountered: