Optional Chaining (aka Existential Operator, Null Propagation)

Claude Pache claude.pache at gmail.com
Tue Feb 9 07:10:34 UTC 2016


> Le 8 févr. 2016 à 01:16, Bergi <a.d.bergi at web.de> a écrit :
> 
> Claude Pache wrote:
>> 
>>> .?
>>> (?)
>>> [?]
>> 
>> 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.
> 
> Totally agreed.
> 
>> * 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).
> 
> I agree here as well, it does feel out of place, and `?[…]`/`?(…)` would feel a lot more natural. Given that those are not feasible for parsing however, I would still prefer them
> 
> obj[?expr]
> func(? …args)
> new C(? …args)
> 
> over the proposed alternative
> 
> obj?.[expr]
> func?.(…args)
> new C?.(…args)
> 
> where the placement of the dot is just horrifying my eyes.

Personally, I'm less attached to aesthetics than legibility.

> Maybe we could at least use some other character instead of the dot?
> 
> obj?:[expr]
> func?:(…args)
> new C?:(…args)
> 
> might bear too much resemblance to the ternary, but imo the colon fits better than the dot here.

Placing the colon before the interrogation mark might also work (at the condition we don’t intend to use `?` as a prefix operator):

```js
obj:?.prop     // (not needed, for symmetry)
obj:?[expr]
func:?(...args)
```

—Claude



More information about the es-discuss mailing list