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
Napi::Value can be empty. Many of N-API will return napi_invalid_arg on empty Napi::Value as a parameter (e.g. napi_get_property). We can introduce a Maybe value box to node-addon-api to early detect these conditions and provide a clean way to indicate whether a parameter Napi::Value is optional.
Yeah, there is already an IsEmpty on Napi::Value. Still, there is no ability to provide a strong type guarantee that a Napi::SomeType is absolutely not empty, i.e. compiler will complain about the code on build time.
If Maybe is not an appropriate name for the situation, I'm ok with another name that fits.
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_valueauto tsfn = ThreadSafeFunction::New(env, callback, resource_name, 1, 1);
With optional boxes, we can make Napi::Value not constructible in the way above, and use Optional::Empty instead:
auto tsfn = ThreadSafeFunction::New(env, Napi::Optional::Empty(), 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.
Activity
legendecas commentedon Mar 26, 2020
I'd believe this can be a better solution for #544.
devsnek commentedon Mar 26, 2020
Napi::Value already provides IsEmpty(). Maybe<> in the V8 API is used to represent things that throw, not things that are optional.
legendecas commentedon Mar 27, 2020
Yeah, there is already an
IsEmptyonNapi::Value. Still, there is no ability to provide a strong type guarantee that aNapi::SomeTypeis absolutely not empty, i.e. compiler will complain about the code on build time.If
Maybeis not an appropriate name for the situation, I'm ok with another name that fits.[-]Maybe Value Boxes[/-][+]Optional Value Boxes[/+]devsnek commentedon Mar 27, 2020
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.
legendecas commentedon Apr 1, 2020
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:
With optional boxes, we can make
Napi::Valuenot constructible in the way above, and useOptional::Emptyinstead:If we do have a callback value:
Or make it implicit:
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.
github-actions commentedon Dec 15, 2020
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.
github-actions commentedon Jun 9, 2021
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.