-
Notifications
You must be signed in to change notification settings - Fork 5
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
Use ".?" operator #2
Comments
I don’t understand what is exactly the issue you are trying to resolve.
The current syntax uses a simple lookahead in the lexical grammar to avoid conflict. By swapping the characters, you just avoid that lookahead. I’m not sure that that alone "greatly simplifies parsers job". Or am I missing something?
The current proposal has already a working-although-weird expansion to arrays and functions. |
parser should work hard so we would work less harder. |
I’m with @nhz-io in thinking the syntax is weird. IMHO the weirdier is I propose another candidate which is obj?? => (typeof obj !== 'undefined' && obj !== null && obj !== undefined)
obj??.key => ((obj??) ? obj.key : undefined)
obj??[key] => ((obj??) ? obj[key] : undefined)
fun??(...a) => ((fun??) ? fun(...a) : undefined)
obj.fun??(...a) => ((obj.fun??) ? obj.fun(...a) : undefined)
// and obviously...
obj??.fun??( ...a ) => ((obj??) && (obj.fun??) ? : obj.fun(...a) : undefined) |
Yeah, i had this one in mind as well, this is actually cleanest approach and no collisions with anything and no lookahead |
This |
I really like this
Yeah but when it comes to propose new syntax, I think it's good to keep in mind that the less hard the parser will have to work, the less impact on performance this syntax will have, and the more chance it will have to be adopted. |
The |
@claudepache If you’re talking about this then… what about this(wild idea): a ?? b => ((a??) ? a : b) |
@claudepache Actually |
@claudepache Argh a ?? b => ((a??) ? a : b)
a??b => ((a??) ? a : b)
a??[k] => ((a??) ? a : [k]) // this or…
=> ((obj??) ? obj[key] : undefined) // this? |
Also used in Swift and in PHP 7. (The
That is approximatively the semantics of the null-coalescing operator, which is completely different from optional chaining. You can’t indeed use the same operator for both operations. |
That part was just to make it clear the possible collisions. |
Swift language has these proposed tokens Also, I suppose that only adding an The only problem being the ternary operator, which Swift in contrast to coffee script has left for your free usage. So we should try to make it work regardless, in my opinion. |
The tokens a?[b] + c: d // interpreted as: a ? ([b] + c) : d (whose meaning cannot be changed) and: a?[b] + c; // should be interpreted as: (a?[b]) + c; Note in particular the difference of precedence level between The syntax |
Hey @claudepache, Thanks for your response!
IMHO this should not be allowed. I understand that it is a breaking change, thus it may not be accepted, but for the most part, the vagueness of this syntax is mostly harming and should be avoided. Also, a simple restriction of making this symbol whitespace intolerant, will eliminate most syntax obscurities. I suggest adding to the token types file 3 new tokens that will be parsed with question special readToken function (similar to that of dot ). It will check for following character being either In the more general sense, it means that the new tokens are preceding the ternary operator, and that lines of the sort you've specified above will be syntax error for that implementation. |
Backward compatibility is quite a hard requirement on the web. There are very plausible use cases such as |
Hmm yeah I now realised the problem with that example. If that's the case then the only possible syntax is one that allows for the ternary operator to have precedence over the optional chaining. or as you've suggested have a non conflicting syntax. What do you think of changing the following line https://github.com/babel/babylon/blob/master/src/parser/expression.js#L158 to a condition where if there is no colon found you will than try to parse optional subscript? As far as I see on the current babylon implementation, this is not something that is thought of as bad implementation. My main argument for preferring this syntax over another is that it fits well with the non optional member expression. Also it is to say that we are not optimizing before checking the impact of said change on the parser's performance. On the browser implementation end, we could optimize by other ways like making sure that those obscure sections are easily resolved by adding parenthesis on the expression. |
C# also has |
I like the idea of |
@hax how would you handle array lookup then? |
@hax Does C# uses |
@Alxandr See @yuchi 's comment: #2 (comment) |
The issue with |
@claudepache C# uses Example: var firstAgeOr5 = people?[0]?.age ?? 5; IDEone link: https://ideone.com/0IyxDW |
@claudepache Yes I agree it's better to stick on similar syntax but |
@claudepache And I remember I have seen a language use Anyway, @yuchi 's proposal is eventually |
Note there are other options in the old discussion at https://esdiscuss.org/topic/existential-operator-null-propagation-operator includes |
To confirm, they do not. Array literals/expressions in C# use |
Ah. Sorry, I misread. Didn't see that the question was of array literals. |
Using C# has both |
Nevermind, I now see the lookahead being referred to is to make an exception for parsing |
"x?.3:0" is valid ternary but not valid chaining operator (orphan ":") so
that could be solved with ahead parsing. Which makes the parsing a bit more
complex (that's where I got lost when trying to modify babel's parser), but
we should be able to keep the operator that way. I didn't find hard
conflicts making breaking compatibility.
As an alternative, I obviously prefer ".?" (and maybe its brother "]?").
…On Tue, May 2, 2017 at 8:28 AM Darien Maillet Valentine < ***@***.***> wrote:
Questions of preference for a particular token aside, x?.3:0 is currently
a valid ternary expression and this proposal would change that as currently
specced. I’m pretty sure that there’s never been a backwards incompatible
change made to the lexical grammar in JavaScript’s 22 years, and I doubt it
would be considered justified here — "don’t break the web" is a pretty high
priority I think. I would second @yuchi <https://github.com/yuchi>’s ??
suggestion as a way to avoid this while still giving the operator a pretty
intuitive symbol.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#2 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AANEMzxppd3F8hHEX4PpL6xk4-1zro7hks5r1s0HgaJpZM4HUMfj>
.
|
Personally I think
but
The second one feels a bit awkward, and with that interpretation it'd also be redundant unless there are multiple levels of chaining ( |
So do I, and it is the reason why that form was chosen rather than this one. But sure, if there is feedback from implementors that this is too hard to parse, alternative syntaxes will be considered. |
@bathos |
@Wedvich @claudepache I agree with the syntax, not with hard to implement. This is a sample of lookahead If is possible with RegExp, it is possible programmatically. |
@leodutra Those were not backward-incompatible (except for an edge-case involving |
@leodutra There is a distinction between the lexical and syntactic grammars in ES. If you need information about an arbitrarily distant token in order to lex the token you’re "at", that is "out of band" to the lexical grammar and must be achieved by switching goal symbols from the parser (lexing and parsing are tightly coupled in ES > 2). If that were introduced here, then one would need to add additional goal symbols to the lexical grammar. That has been done before — first for regular expression literals and second for template spans. But each additional lexical ambiguity expands the set of goals potentially by a lot. In this case I’m pretty sure that would mean adding four new ones:
Edit: on reflection, it might mean adding only two. The |
@claudepache Ah... you are speaking about not breaking existing code. Right, but no proposal here, till now, would break existing code. It would not work in old engines, as let, const and lambdas... that's what I mean. If we are not ok with lookaheads, only a single symbol or an totally new symbol for JS can evade lookaheads. Pick yours: #, @, §, &, ... user#props ? true : false I don't care about the lexer/parser. Its devs shall sweat blood and pain, as any dev shall do for top grade stuff. The important part is JavaScript pushes the best of known C-like languages. C# An Optional proposal would be very welcome as, widely, most functional languages relies on this for feature rich optional chaining. |
By the way, I'm not a C# fan. But I can't deny the greatness of their chaining and Lisp (Although this last one is constantly raped by bad approaches). More about Option, for those who may not know it. |
I don’t think it’s a question of implementation difficulty? Complexity maybe. What you described implied a "bigger touch" to the lexical grammar than the proposal as it currently stands; meanwhile |
@bathos I agree Option Type is a greedy eye on conditionals. C# has different meaning for An option for |
There is already no space allowed between the two chars. The token |
I'm closing this issue, as I won’t change the syntax, and it is not possible to satisfy everyone anyway. Any choice has its own pros and cons. If you have a strong feeling about the result, you may discuss it either on es-discuss or on the official repo. |
Created this method which you might find useful (it's type-safe btw):
And you can use it like this:
|
By swapping characters you can greatly simplify parsers job and eliminate every potential conflict.
It allows expansion to arrays and function even if final syntax would be weird :)
The text was updated successfully, but these errors were encountered: