Skip to content
This repository was archived by the owner on Jan 28, 2023. It is now read-only.

Normative: Switch syntax to ??: #23

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const showSplashScreen = response.settings?.showSplashScreen || true; // Potenti
The nullary coalescing operator is intended to handle these cases better and serves as an equality check against nullary values (`null` or `undefined`).

## Syntax
*Base case*. If the expression at the left-hand side of the `??` operator evaluates to undefined or null, its right-hand side is returned.
*Base case*. If the expression at the left-hand side of the `??:` operator evaluates to undefined or null, its right-hand side is returned.

```javascript
const response = {
Expand All @@ -49,13 +49,17 @@ const response = {
}
};

const undefinedValue = response.settings?.undefinedValue ?? 'some other default'; // result: 'some other default'
const nullValue = response.settings?.nullValue ?? 'some other default'; // result: 'some other default'
const headerText = response.settings?.headerText ?? 'Hello, world!'; // result: ''
const animationDuration = response.settings?.animationDuration ?? 300; // result: 0
const showSplashScreen = response.settings?.showSplashScreen ?? true; // result: false
const undefinedValue = response.settings?.undefinedValue ??: 'some other default'; // result: 'some other default'
const nullValue = response.settings?.nullValue ??: 'some other default'; // result: 'some other default'
const headerText = response.settings?.headerText ??: 'Hello, world!'; // result: ''
const animationDuration = response.settings?.animationDuration ??: 300; // result: 0
const showSplashScreen = response.settings?.showSplashScreen ??: true; // result: false
```

### Why the `??:` token?

The initial proposal here used the token `??`, which is similar to what several other programming languages do for the same purpose. This proposal use `??:` instead, so that similar operators can be used by the [optional chaining proposal](https://github.com/TC39/proposal-optional-chaining) (e.g., `??[`). See [the past discussion](https://github.com/tc39/proposal-optional-chaining/issues/34) on this topic for more details.

## Notes
While this proposal specifically calls out `null` and `undefined` values, the intent is to provide a complementary operator to the [optional chaining operator](https://github.com/TC39/proposal-optional-chaining). This proposal will update to match the sementics of that operator.

Expand Down
10 changes: 5 additions & 5 deletions spec.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@

<emu-intro id=sec-intro>
<h1>Introduction</h1>
<p>This document specifies the nullish coalescing operator `??`. See <a href="https://github.com/gisenberg/proposal-nullish-coalescing">the explainer</a> for an introduction.</p>
<p>This document specifies the nullish coalescing operator `??:`. See <a href="https://github.com/gisenberg/proposal-nullish-coalescing">the explainer</a> for an introduction.</p>
<p>The main design decisions made in this specification are:
<ol>
<li>The right argument of `??` is evaluated only if needed ("short circuiting").</li>
<li>`??` has the same precedence as `||`.</li>
<li>The right argument of `??:` is evaluated only if needed ("short circuiting").</li>
<li>`??:` has the same precedence as `||`.</li>
<li>The right argument is selected if the left argument is `null` or `undefined`.</li>
</ol>
</p>
Expand All @@ -25,14 +25,14 @@ <h1>Syntax</h1>
LogicalORExpression[In, Yield, Await] :
LogicalANDExpression[?In, ?Yield, ?Await]
LogicalORExpression[?In, ?Yield, ?Await] `||` LogicalANDExpression[?In, ?Yield, ?Await]
<ins>LogicalORExpression[?In, ?Yield, ?Await] `??` LogicalANDExpression[?In, ?Yield, ?Await]</ins>
<ins>LogicalORExpression[?In, ?Yield, ?Await] `??:` LogicalANDExpression[?In, ?Yield, ?Await]</ins>
</emu-grammar>
</emu-clause>

<emu-clause id="sec-nullish-coalescing-evaluation">
<h1>Runtime Semantics: Evaluation</h1>

<emu-grammar>LogicalORExpression : LogicalORExpression `??` LogicalANDExpression</emu-grammar>
<emu-grammar>LogicalORExpression : LogicalORExpression `??:` LogicalANDExpression</emu-grammar>
<emu-alg>
1. Let _lref_ be the result of evaluating |LogicalORExpression|.
1. Let _lval_ be ? GetValue(_lref_).
Expand Down