tc39/proposal-decorators

Support changing the backing field used by auto-accessors

zaygraveyard opened this issue · 2 comments

The README stats that the getter and setter defined by the auto-accessors "default to getting and setting a value on a private slot.", so why not allow the use of a different backing field?

The suggestion

Allow accessor to take the backing field name as parameter: accessor(fieldName) accessorName.

accessor(#x) x = 1; would be equivalent to accessor x = 1; except that with the former #x would be accessible syntactically in the class.

The following example would be valid:

class C {
  accessor(#x) x = 1;

  method() {
    this.#x = 456;
  }
}

The backing field can also be public for those who want to avoid private class fields:

class C {
  accessor(_x) x = 1;
}

The above would roughly desugar to:

class C {
  _x = 1;
  get x() {
    return this._x;
  }
  set x(val) {
    this._x = val;
  }
}

This would probably also solve tc39/proposal-grouped-and-auto-accessors#10

Is this the right place?

This proposal is stage 3, but there are no native engines with this shipped yet, as far as I know, not even behind a flag:

https://caniuse.com/decorators

Should this be part of the Grouped Accessors and Auto-Accessors proposal?
Should this be a new proposal that build on top of this one?

This is my first time contributing to discussions on TC39 proposals 😅

pzuraq commented

hey @zaygraveyard, thank you for the suggestion! This is an interesting idea, but I believe this is out of scope for changes that can occur in stage 3 of a proposal.

Limited: only those deemed critical based on implementation experience

See the proposal process document for more information there. That said, this change would in be incremental and could be added in a future proposal. I think that it is definitely related to the Grouped Accessors proposal, so I would reopen discussion of it over there since that may be a place where this can be more thoroughly explored.

Thank you for the quick reply! 😄
I will open this issue in the Grouped Accessors proposal.

Closing this issue as it is out of scope.