From 31d3e1f1c1049c0deb0ab3044ad84bb3cff93ad0 Mon Sep 17 00:00:00 2001 From: Daniel Ehrenberg Date: Fri, 23 Feb 2018 21:12:49 +0100 Subject: [PATCH] Normative: Switch syntax to ??. Follows the conclusion of the discussion at https://github.com/tc39/proposal-optional-chaining/issues/34 --- README.md | 78 +++++++++++++++++++++++-------------------------------- spec.html | 50 ++++++++++++++--------------------- 2 files changed, 52 insertions(+), 76 deletions(-) diff --git a/README.md b/README.md index 3c1c808..a164b68 100644 --- a/README.md +++ b/README.md @@ -25,18 +25,18 @@ var fooValue = fooInput ? fooInput.value : undefined The Optional Chaining Operator allows a developer to handle many of those cases without repeating themselves and/or assigning intermediate results in temporary variables: ```javascript -var street = user.address?.street -var fooValue = myForm.querySelector('input[name=foo]')?.value +var street = user.address??.street +var fooValue = myForm.querySelector('input[name=foo]')??.value ``` The call variant of Optional Chaining is useful for dealing with interfaces that have optional methods: ```js -iterator.return?.() // manually close an iterator +iterator.return??() // manually close an iterator ``` or with methods not universally implemented: ```js -if (myForm.checkValidity?.() === false) { // skip the test in older web browsers +if (myForm.checkValidity??() === false) { // skip the test in older web browsers // form validation fails return; } @@ -54,44 +54,45 @@ The following languages have a similar feature. We haven’t checked whether the ## Syntax -The Optional Chaining operator is spelled `?.`. It may appear in three positions: +The Optional Chaining operator is spelled `??`. It may appear in three positions: ```javascript -obj?.prop // optional static property access -obj?.[expr] // optional dynamic property access -func?.(...args) // optional function or method call +obj??.prop // optional static property access +obj??[expr] // optional dynamic property access +func??(...args) // optional function or method call ``` -### Notes -* In order to allow `foo?.3:0` to be parsed as `foo ? .3 : 0` (as required for backward compatibility), a simple lookahead is added at the level of the lexical grammar, so that the sequence of characters `?.` is not interpreted as a single token in that situation (the `?.` token must not be immediately followed by a decimal digit). +### Why two question marks instead of one? + +An earlier version of this proposal used `??.` for the optional chaining operator, with `??[` for dynamic property access and `??(` for optional function/method calls. The switch to two question marks allows for more consistency and the ability to omit the surprising `.` in the latter two cases. See [the past discussion](https://github.com/tc39/proposal-optional-chaining/issues/34) on this topic for more details. ## Semantics ### Base case -If the operand at the left-hand side of the `?.` operator evaluates to undefined or null, the expression evaluates to undefined. Otherwise the targeted property access, method or function call is triggered normally. +If the operand at the left-hand side of the `??` operator evaluates to undefined or null, the expression evaluates to undefined. Otherwise the targeted property access, method or function call is triggered normally. Here are basic examples, each one followed by its desugaring. (The desugaring is not exact in the sense that the LHS should be evaluated only once.) ```js -a?.b         // undefined if `a` is null/undefined, `a.b` otherwise. +a??.b         // undefined if `a` is null/undefined, `a.b` otherwise. a == null ? undefined : a.b -a?.[x]       // undefined if `a` is null/undefined, `a[x]` otherwise. +a??[x]       // undefined if `a` is null/undefined, `a[x]` otherwise. a == null ? undefined : a[x] -a?.b()       // undefined if `a` is null/undefined +a??.b()       // undefined if `a` is null/undefined a == null ? undefined : a.b() // throws a TypeError if `a.b` is not a function                             // otherwise, evaluates to `a.b()` -a?.()       // undefined if `a` is null/undefined +a??()       // undefined if `a` is null/undefined a == null ? undefined : a() // throws a TypeError if `a` is neither null/undefined, nor a function             // invokes the function `a` otherwise ``` ### Short-circuiting -If the expression on the LHS of `?.` evaluates to null/undefined, the RHS is not evaluated. This concept is called *short-circuiting*. +If the expression on the LHS of `??.` evaluates to null/undefined, the RHS is not evaluated. This concept is called *short-circuiting*. ```js -a?.[++x]         // `x` is incremented if and only if `a` is not null/undefined +a??[++x]         // `x` is incremented if and only if `a` is not null/undefined a == null ? undefined : a[++x] ``` @@ -100,7 +101,7 @@ a == null ? undefined : a[++x] In fact, short-circuiting, when triggered, skips not only the current property access, method or function call, but also the whole chain of property accesses, method or function calls directly following the Optional Chaining operator. ```js -a?.b.c(++x).d  // if `a` is null/undefined, evaluates to undefined. Variable `x` is not incremented. +a??.b.c(++x).d  // if `a` is null/undefined, evaluates to undefined. Variable `x` is not incremented.               // otherwise, evaluates to `a.b.c(++x).d`. a == null ? undefined : a.b.c(++x).d ``` @@ -116,7 +117,7 @@ Let’s call *Optional Chain* an Optional Chaining operator followed by a chain An Optional Chain may be followed by another Optional Chain. ```js -a?.b[3].c?.(x).d +a??.b[3].c??(x).d a == null ? undefined : a.b[3].c == null ? undefined : a.b[3].c(x).d // (as always, except that `a` and `a.b[3].c` are evaluated only once) ``` @@ -126,7 +127,7 @@ a == null ? undefined : a.b[3].c == null ? undefined : a.b[3].c(x).d Parentheses limit the scope of short-circuting: ```js -(a?.b).c +(a??.b).c (a == null ? undefined : a.b).c ``` @@ -138,7 +139,7 @@ Note that, whatever the semantics are, there is no practical reason to use paren Because the `delete` operator is very liberal in what it accepts, we have that feature for free: ```js -delete a?.b +delete a??.b // delete (a == null ? undefined : a.b) // that *would* work if `? :` could return a Reference... a == null ? undefined : delete a.b // this is what we get, really ``` @@ -147,13 +148,13 @@ a == null ? undefined : delete a.b // this is what we get, really The following are not supported due to a lack of real-world use cases: -* optional construction: `new a?.()` -* optional template literal: ``a?.`{b}` `` -* constructor or template literals in/after an Optional Chain: `new a?.b()`, ``a?.b`{c}` `` +* optional construction: `new a??()` +* optional template literal: ``a??`{b}` `` +* constructor or template literals in/after an Optional Chain: `new a??b()`, ``a??.b`{c}` `` The following is not supported, although it has some use cases; see [Issue #18](//github.com/tc39/proposal-optional-chaining/issues/18) for discussion: -* optional property assignment: `a?.b = c` +* optional property assignment: `a??.b = c` All the above cases will be forbidden by the grammar or by static semantics so that support might be added later. @@ -165,28 +166,13 @@ All the above cases will be forbidden by the grammar or by static semantics so t
-
obj?.[expr] and func?.(arg) look ugly. Why not use obj?[expr] and func?(arg) as does <language X>? - -
- -We don’t use the `obj?[expr]` and `func?(arg)` syntax, because of the difficulty for the parser to efficiently distinguish those forms from the conditional operator, e.g., `obj?[expr].filter(fun):0` and `func?(x - 2) + 3 :1`. - -Alternative syntaxes for those two cases each have their own flaws; and deciding which one looks the least bad is mostly a question of personal taste. Here is how we made our choice: - -* pick the best syntax for the `obj?.prop` case, which is expected to occur most often; -* extend the use of the recognisable `?.` sequence of characters to other cases: `obj?.[expr]`, `func?.(arg)`. - -As for <language X>, it has different syntactical constraints than JavaScript because of <some construct not supported by X or working differently in X>. - - - -
Why does (null)?.b evaluate to undefined rather than null? +
Why does (null)??.b evaluate to undefined rather than null?
-Neither `a.b` nor `a?.b` is intended to preserve arbitrary information on the base object `a`, but only to give information about the property `"b"` of that object. If a property `"b"` is absent from `a`, this is reflected by `a.b === undefined` and `a?.b === undefined`. +Neither `a.b` nor `a??.b` is intended to preserve arbitrary information on the base object `a`, but only to give information about the property `"b"` of that object. If a property `"b"` is absent from `a`, this is reflected by `a.b === undefined` and `a??.b === undefined`. -In particular, the value `null` is considered to have no properties; therefore, `(null)?.b` is undefined. +In particular, the value `null` is considered to have no properties; therefore, `(null)??.b` is undefined. @@ -198,7 +184,7 @@ See [Issue #3 (comment)](https://github.com/tc39/proposal-optional-chaining/issu -
In a?.b.c, if a.b is null, then a.b.c will evaluate to undefined, right? +
In a??.b.c, if a.b is null, then a.b.c will evaluate to undefined, right?
@@ -206,11 +192,11 @@ No. It will throw a TypeError when attempting to fetch the property `"c"` of `a. The opportunity of short-circuiting happens only at one time, just after having evaluated the LHS of the Optional Chaining operator. If the result of that check is negative, evaluation proceeds normally. -In other words, the `?.` operator has an effect only at the very moment it is evaluated. It does not change the semantics of subsequent property accesses, method or function calls. +In other words, the `??.` operator has an effect only at the very moment it is evaluated. It does not change the semantics of subsequent property accesses, method or function calls. -
In a deeply nested chain like `a?.b?.c`, why should I write `?.` at each level? Should I not be able to write the operator only once for the whole chain?
+
In a deeply nested chain like `a??.b??.c`, why should I write `??.` at each level? Should I not be able to write the operator only once for the whole chain?
diff --git a/spec.html b/spec.html index ede8cbc..6e2ef9c 100644 --- a/spec.html +++ b/spec.html @@ -22,7 +22,7 @@

Scope

This is the spec text of the Optional Chaining proposal in ECMAScript.

-

For the syntax, we use the `?.` token, with a lookahead at the level of the lexical grammar that allows to discriminate between `a?.b` (optional chaining) and `a?.3:0` (conditional operator, whose meaning cannot be changed due to backward compatibility constraints).

+

For the syntax, we use the `??.` token. An earlier version of this proposal used `?.`

An early version of this proposal used a Nil reference to express short-circuiting. This one is based on syntax only.

@@ -50,11 +50,7 @@

Punctuators (11.7<

Syntax

- OptionalChainingPunctuator :: - `?.` [lookahead <! DecimalDigit - - - OtherPunctuator :: one of + Punctuator :: `{` `(` `)` `[` `]` `.` `...` `;` `,` `<` `>` `<=` `>=` @@ -68,17 +64,7 @@

Syntax

`?` `:` `=` `+=` `-=` `*=` `%=` `**=` `<<=` `>>=` `>>>=` `&=` `|=` `^=` `=>` - - Punctuator :: - OptionalChainingPunctuator - OtherPunctuator - - DivPunctuator :: - `/` - `/=` - - RightBracePunctuator :: - `}` + `??.` `??(` `??[`