Support nullish coalescing operator ?? in mathjs expression language
Describe the suggested feature
Currently x ?? 0 is a syntax error. It would be handy if this meant the nullish coalescing operator denoted by ?? in JavaScript. For example, one could then do object lookup with a fallback : {foo: 7, bar: 3}["baz"] ?? 0.
Current workaround
nullish(x, y) = (x == null or x == undefined) ? y : x; nullish(null, 0)
I am not aware of a short way to test for null or undefined unlike x == null in JavaScript which is true if x is either null or undefined; in mathjs expression language it is true when x is null and false when x is undefined.
Having the nullish coalescing operator would be great!
Hey @josdejong
I would like to give this a shot
As the suggested syntax enhancement has been approved by the head of MathJS, a PR is welcome. Of course be sure that the change does not disrupt the ternary operator x ? 1 : 0 and add tests for variants like x ?? 0, x??0, etc.
Thanks @ikemHood , please let us know if you need any pointers.
I'd love to work on this as I have experience with parsers and expression evaluators. I'll add ?? support by updating the tokenizer, parser, and evaluator with proper precedence and tests.
I believe @ikemHood is working on it; you should check with that person to avoid duplicating effort.
Yes. @gwhitney. made a PR (#3497)
waiting for a review
I have experience in JavaScript. I want to fix this problem.
@AbiodunVlad, we appreciate your interest in MathJS and in this issue. However, as @ikemHood has submitted a PR for it that seems at least for the time being to be on the right track, we would not recommend your duplicating effort by working on this issue. You may find others that are of interest to you.
Published now in v14.8.0