Optional Chaining (aka Existential Operator, Null Propagation)
/#!/JoePea
joe at trusktr.io
Thu Feb 4 18:57:45 UTC 2016
Hello Claude, you prefer `?.` over `.?` as an implementor (I understand
what you said about the parsing). But I think as and end user of the
syntax, `.?` over `?.` makes more sense as it is easier to distinguish from
the ternary operator with a float, as in `x?.3:0` (we know a numerical key
can't be accessed with the dot operator currently) and `x?.foo:0`. If we
assume the existential operator doesn't allow numerical property access,
then `x.?3` is *obviously* an error (not so obvious with `?.`), and we
won't expect the ternary operator. Although, `x.?3` also introduces the
interesting possibility of allowing numerical property access as that also
can't be confused with a float-in-a-ternary like `x?.3` can, which would
mean that with `x.?3` we would not need to use `[]` to access numerical
properties (which be convenient in the use case of the existential
operator!). Something like `x.?.3` would have to fail as a syntax error.
```
let a = [1,2,{n:1}]
console.log(a[2]) // {n:1}
console.log(a.?2) // {n:1}
console.log(a.?.2) // SyntaxError
console.log(a.?2.3) // SyntaxError: Unexpected number (the 3)
console.log(a.?2.n) // 1
console.log(a.?2.?n) // 1
```
/#!/JoePea
On Feb 4, 2016 10:06 AM, "Claude Pache" <claude.pache at gmail.com> wrote:
>
> Le 4 févr. 2016 à 17:47, John Lenz <concavelenz at gmail.com> a écrit :
>
>
> [...]
>
>
> Waldemar's example makes the problem obvious but I think we could do use,
> which I think is preferable to the proposed:
>
> .?
> (?)
> [?]
>
>
> Yes, that syntax is possible. Whether it is preferable is a question of
> taste. Personally, I don’t like it:
>
> * I slightly prefer `?.` over `.?` for the following reason: The `?.`
> token may be conceptually separated in two, first the question mark which
> checks whether the expression at its left evaluates to null/undefined (and
> orders to stop processing if it is the case); then the dot which proceeds
> with property lookup.
>
> * I find that the question mark inside the brackets is out of place, as it
> isn’t part of the arguments (for function call) or of the expression
> defining the key (for property access).
>
>
>
>> [...]
>
>
> yes, I meant the equivalent to:
>
> x ?: value
> x == null ? x : value
>
>
> I assume you meant: `x != null ? x : value`
>
> Note that this is a completely different operator. For the proposed
> optional chaining operator, we stop processing when the LHS is
> null/undefined; while for the `?:` null-coalescing operator, it is the
> other way round. Also, the precedence is not the same.
>
> I have wilfully restricted the scope of my proposal to optional chaining
> only, because of its intrinsic technical complexity.
>
> —Claude
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20160204/edc763ee/attachment-0001.html>
More information about the es-discuss
mailing list