-
Notifications
You must be signed in to change notification settings - Fork 465
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Optional Value Boxes #690
Comments
I'd believe this can be a better solution for #544. |
Napi::Value already provides IsEmpty(). Maybe<> in the V8 API is used to represent things that throw, not things that are optional. |
Yeah, there is already an If |
Adding Maybe wouldn't fix the issue that there's no way to guarantee a value is not empty because the Value in the Maybe can still be empty. I do agree it would be cool if it were possible but that might be outside the bounds of the language we've chosen to work with here. |
The idea is to distinguish empty-able parameters and return values at build time. In the example of #544, originally we have to use a trick to achieve what we expect: Function callback; // this one is empty since it's not initialized with any napi_value
auto tsfn = ThreadSafeFunction::New(env, callback, resource_name, 1, 1); With optional boxes, we can make auto tsfn = ThreadSafeFunction::New(env, Napi::Optional::Empty(), resource_name, 1, 1); If we do have a callback value: Napi::Function callback = Napi::Function::New(env, some_callback);
auto tsfn = ThreadSafeFunction::New(env, Napi::Optional::Just(callback), resource_name, 1, 1); Or make it implicit: Napi::Function callback = Napi::Function::New(env, some_callback);
auto tsfn = ThreadSafeFunction::New(env, callback, resource_name, 1, 1); In this way, we can make our API stronger than just empty-able Napi::Value, with which compiler doesn't complain if it's empty where it should not be empty.
|
This issue is stale because it has been open many days with no activity. It will be closed soon unless the stale label is removed or a comment is made. |
This issue is stale because it has been open many days with no activity. It will be closed soon unless the stale label is removed or a comment is made. |
Napi::Value
can be empty. Many of N-API will return napi_invalid_arg on emptyNapi::Value
as a parameter (e.g.napi_get_property
). We can introduce aMaybe
value box to node-addon-api to early detect these conditions and provide a clean way to indicate whether a parameterNapi::Value
is optional.The text was updated successfully, but these errors were encountered: