tc39 / proposal-nullish-coalescing

Nullish coalescing proposal x ?? y
https://tc39.github.io/proposal-nullish-coalescing/
1.23k stars 23 forks source link

Null-coalescing assignment operator #57

Closed Xstoudi closed 4 years ago

Xstoudi commented 4 years ago

I open this issue to talk about nullish-coalescing assignment operator (??=) because I think it's maybe related to the nullish-coalescing operator. I was suprised to not see it in this current proposal because it could be really useful (as useful as a syntaxic sugar can be, at least).

The following example make use of proposal-class-fields. Something like that:

class Foo {
    #bar;
    get Bar() {
        this.#bar = this.#bar ? this.#bar : new Runner();
        return this.#bar;
    }
}

or

```js
class Foo {
    #bar;
    get Bar() {
        this.#bar =  this.#bar ?? new Runner();
        return this.#bar;
    }
}

would become

class Foo {
    #bar;
    get Bar() {
      return this.#bar ??= new Runner();
    }
}

As prior art:

What do you think about it?

jridgewell commented 4 years ago

See https://github.com/tc39/proposal-logical-assignment (last discussed in 2018-03 meeting). The consensus was that the proposal could not advance until ?? landed (we didn't want to add ||= without ??= at the same time).

We'll be able to discuss it again soon! 😃

Xstoudi commented 4 years ago

What a fast answer! I'm happy to read that, I'll star the repo to participate when the time comes.