feat: add nullish coalescing operator support#3497
feat: add nullish coalescing operator support#3497gwhitney merged 23 commits intojosdejong:developfrom
Conversation
|
Here's a first review; there may be more comments later, and @josdejong may weigh in as well. Jos, as the PR stands the nullish coalescing operator is only included in the full mathjs library, not the numbers-only library. Should it be in the latter as well? Also, in JavaScript, it is illegal to mix nullish coalescing with logical "and" as well as with logical "or," unless the expression is parenthesized. I think it is because the sense of the JavaScript designers was that the precedence between these operators is unclear, and so it might be difficult to understand the intention of an expression that was written without parentheses. As this PR stands, an expression like Oh and also @ikemHood you may not have noticed that there is a list of operators and their precedences at docs/expressions/syntax.md. That needs to be updated in this PR. Thanks. |
|
Thanks for reviewing Glen! About the precedence of
I don't fully understand, I would think you can just give the operator a different predecence like the PR does right now (like lower than |
Basically the discussion in tc39/proposal-nullish-coalescing#26 about what the precedence of ?? should be was not converging, so the TC39 decided to punt and simply make it illegal to combine ?? with || or && at the same level without parentheses, to make the conversation about which way expressions like However, having now read over all the debates in some detail, I actually find the arguments that To summarize the options that seem reasonable to me, in no particular order:
I don't think @ikemHood can reasonably complete this PR until @josdejong makes a decision on precedence, so marking it as draft for now. |
|
It makes a lot of sense to me to both (a) give it a different precedence from About associativity: I think in practice the accociativity doesn't matter (like with addition), but it is good to define it officially I think. Left-to-right makes most sense to me: you pick the first non-nullish default value, going through it from left to right like in @ikemHood do you have any thoughts on this? |
|
And thanks for digging into this @gwhitney , really helpful! |
|
Gwen was detailed. I'd support we give it a precedence higher than exponentiation. |
|
Sounds good, let's go for it :) |
…itional expressions
|
Unless I missed them, there are still no unit tests of |
|
Oh, and also, now that we have chosen a very different precedence for ?? than JavaScript does, that fact should probably be mentioned in the bullet list in the syntax.md. Either there should be a bullet that says "Some operator precedences differ" with a couple of examples if there is more than one such precedence difference (you'll need to compare the tables for the two languages), or if this is the only difference, say "The precedence of the nullish coalescing operator ?? is higher so that e.g. |
Sorry if I wasn't clear. There should be a test/unit-tests/function/logical/nullish.test.js, thanks. I will look at the new docs. |
|
Done, I added a |
|
@gwhitney the last commit fixes the failing CI |
|
@josdejong shouldn't there just be one line in AUTHORS for this contributor? And @ikemHood which of the two addresses would you like to appear? Thanks! |
I'll clean it up when publishing the next version. Apparently @ikemHood has created commits with two different git accounts. |
|
|
@gwhitney you've reviewed this PR before. Are there still open issues or is all your feedback addressed? |
|
@josdejong @ikemHood the remaining concerns I can see are as follows:
but which results in double redispatch (first to for this handler, saving one round of redispatch. My apologies for the large number of iterations of review on this PR, but the addition of a new operator is relatively far-reaching within the design of mathjs, and many of the points raised have only been able to come into focus when prior concerns were dealt with. I've made a strong effort in this comment to collect everything else I can see into a single place. I hope if/when these are all dealt with, the PR will be ready to merge, but I can't 100% promise that there will be nothing further on re-review when these are dealt with. Thanks for understanding! |
|
Thanks for the overview Glen! The PR looks very neat already @ikemHood I hope you'll be able to address the last open ends! |
Yes, done that. |
|
Published now in |
feat: add nullish coalescing operator support
closes #3353