Add get_or_undefined
method to arguments of builtin functions
#1490
Milestone
get_or_undefined
method to arguments of builtin functions
#1490
In the current state of the API, if we want to access a possibly missing argument of a function with a default value of
undefined
, we eitherclone
tounwrap_or_default
or create a newValue::Undefined
andunwrap_of(&undefined)
.I extracted two implementation examples from the codebase:
The first alternative is not ideal, we are either copying primitive types or cloning an
Rc
just to drop it at the end of the function.The second alternative is good, but it gets very repetitive having to always call
get(n).unwrap_of(&undefined)
and always creates anundefined
variable at the beginning of a function.We mainly use the former and sometimes the latter, but it would be ideal to be able to just call
let this_arg = args.get_or_undefined(0);
. The way I think we can optimally solve this is by creating a newArgs
trait, implementing it only for&[JsValue]
and adding this new trait to the prelude, essentially making it public to the user.Also,
the functionShould it? Maybe the overhead of the indirection ofget_or_undefined
should return aCow<JsValue>
to avoid cloning in case the argument exists and we just need the reference.Cow
is greater than the overhead of cloning aJsValue
. We would need to benchmark both options to determine the best.Do you have any other solutions? I'd like to hear what you think.
The text was updated successfully, but these errors were encountered: