-
Notifications
You must be signed in to change notification settings - Fork 75
Dot after question mark #5
Comments
The issue with |
Sigh. Alrighty then. Closing up :) |
I think this should be reconsidered. Such expression is used in many languages already. |
I'm reopening then. Some people(in other issues) have expressed interest in having this debate again... |
The conceptors of C# did apparently face a similar parsing issue around
|
@claudepache you're right about C#. Although it works in Swift but they have their own constraints that make it feasible to disambiguate optional chaining from ternary expression, such as spaces are taken into account, i.e:
It's possible to support the
In case if there is no space before To conclude my take on that:
After spending some time on that I must admit that |
Personally I would separate |
I think consistency is important. IMHO, the This also creates an inconsistency that I haven't seen mentioned. Given the operator Case 1: The operator It seems as if the original idea was to support The ProposalsInitially support
|
A two-char operator is possible, yes. There is however a tension between keeping a better syntax for the common case ( Anyway, although it is technically possible, we should avoid to use the same symbol
Possible alternative: |
About the suggestion to initially support I’m against it. According to statistics in #17, in a sample of CoffeeScript code, there are significant uses of It is best to decide now (I mean, during the next few months) for a syntax for all cases, whether it is uniform or slightly inconsistent with better look for the common case. |
I am talking about the babel plugin. We shouldn't push stuff like
That's alright by me but the current proposal do favor certain syntaxes that are controversial and inconsistent with |
I understand the unfortunate reasons why a?[..] isn't going to happen - but I'm with levithomason here. While a??.b isn't ideal, I think it is at least tolerable if it means we don't have to have a?.[foo] In my opinion, a?.[foo] is pretty confusing as it is easy to accidentally read the ?. operator as two separate operators - especially for us poor souls that have written lots of coffeescript. |
Well, as someone who as written lots of JavaScript, I find CoffeeScript semantics of a ?[b] # in JS: a != null ? a : [b]`
a?[b] # a != null ? a[b] : undefined;
null ?[b] # [b]
null?[b] # syntax error
(null)?[b] # undefined
a = { 'x': 42 }
a ?['x'] # a
a?['x'] # 42 Also: 42 // comment # Math.floor(42/comment)
a ? b : c # a != null ? { b: c } : undefined I’m not saying that we should ignore coffeescripters’ habits. But if you switch between CoffeeScript and JavaScript, you must already know that there are many things that work differently. |
I like @levithomason's proposal for The double-question-mark syntax also nicely parallels nullary-coalescing |
As I said above, for me it is a problem rather than an advantage, because the null-coalescing operator has an opposite meaning. (I know that CoffeeScript uses the same symbol for both operators, but CoffeeScript is somewhat confusing.) |
Eh, sure, I can see the potential for confusion. What about using Otherwise, we need a different two-character combination for optional chaining, since single special characters all present parsing difficulties. I suppose I could get used to something like |
@claudepache thanks for the counter points, I'm in agreement with you. I was also not aware of the complimentary nullary-coalescing operator proposal. Per the stated goal of that project:
I'd be in favor of using the cleanest syntax for optional chaining (perhaps Proposed nullary coalescing updateLooking at this operator in other languages for inspiration, here are some options:
Of these, I'd vote for 2) const val = a??.b |? 'a was null'
// vs
const val = a??.b ?: 'what is happening' Optional |
@tvald the elvis operator should probably mirror the usage of the ternary syntax and hence not be limited to @levithomason your |
If consistent application of This is the set of relevant operators that I'm aware of:
It feels like the set of possibilities for guard/coalesce are currently a mess, but that we want some recognizable parallel with the chaining operator. I think designing each of these operators independently will result in confusion, as they all inhabit corners of the same space. edit 2017/08/08: simplified table by removing extraneous ternary row |
I'm not sure I follow some of the examples in the table. My proposal is |
Nullish guard shorthand ( Two comments above, @Mouvedia argued that elvis ( I'm just documenting the complete space of possible and proposed adjacent operators. |
This is how I would fill in the chart, for optimal symmetry:
(If this is too far astray from the original discussion, I can open a separate issue either here or in the nullary-coalese repo.) |
Using A potential issue, is that |
No, your link just shows an example of custom operator definition. |
I'd be ok with // this is confusing
obj?.foo?.(args).prop
// a little better
obj??.foo??(args).prop |
@claudepache Even if it's not an issue formally, this still seems a bit confusing for developer intuition. I like how the current proposal includes |
If ? must be in the operator then how about a?>.b
a?>[b]
a?>(b) |
@TheNavigateur Actually, I like that one! |
Looks pretty similar to pipeline tho. |
There's another thread that talks about the syntax a bit more: #34 In an informal poll, In any case, should this issue be folded into that one? |
I think |
Here's a proposal which resolves the cases that were considered problematic. // a == null ? undefined : a.b
a?.b;
// (a == null ? undefined : a[d]) + c
(a?[d]) + c
// throws (missing the : part of the ternary)
a ? [d] + c;
// just a backward compatible ternary
a ? [d]+c : d;
// throws (missing the ? part of the ternary)
(a?[d])+c : d;
// throws
e ? a?[d]+c : d;
// OK
e ? (a?[d])+c : d;
// OK
e ? a ? [d]+c : d : f;
e ? (a ? [d]+c : d) : f; The real beauty in that proposal is that you will have an early error in previous ES implementation because the closing parenthesis is not found before the |
That’s an unacceptable ergonomic cost - bracket access needs to be as seamless as dot access. |
I'm not sure if I agree with @ljharb on this stance on ergonomics in general, but I don't see how adding parentheses addresses the grammar issue. You'd still have to read ahead to the closing paren to figure out whether this is ternary or optional chaining, wouldn't you? |
Not sure if it’s too hard to do a lookahead. But if we would have to do a lookahead then there is no need to use parens until ternary expression involved. Then I find it reasonable to use parens to disambiguate the ternary part from the optional chaining. |
One of the concerns here, which ruled out |
Though |
Might only be as much conflict as |
I'm not sure But I certainly agree with @ljharb that requiring parens would be unacceptable. Perhaps discussion about |
Wouldn't be the first time. |
@Mouvedia that should have been unacceptable, and could have been avoided. That's not a precedent to knowingly add another huge footgun along with new syntax. |
There's some kind of poll on #51. |
Why not think about it as a "?." operator with its own, slightly different, semantics, rather than spending a few years coming up a with a pristine proposal that won't satisfy all anyway? |
Why don't we just close this issue and stick to the current proposal? |
Closes tc39#5.
I'm very very glad to see this become resurrected. One thing I'm curious about is why you chose to have a dot after the question mark in case of function invocation and why you have a dot between question marks and square bracket notation. It seems strange to me, but perhaps i'm too biased by having written too much CoffeeScript.
The text was updated successfully, but these errors were encountered: