Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Glob assignment of private fields fails #966

Open
bbrk24 opened this issue Feb 9, 2024 · 4 comments
Open

Glob assignment of private fields fails #966

bbrk24 opened this issue Feb 9, 2024 · 4 comments
Labels
bug Something isn't working

Comments

@bbrk24
Copy link
Contributor

bbrk24 commented Feb 9, 2024

The following code attempts to incorrectly destructure this:

class Foo
  #x = 0
  #y = 0

  clone()
    return = new Foo
    return.value{#x,#y} = @
class Foo {
  #x = 0;
  #y = 0;

  clone() {
    let ret;
    ret = new Foo();
    ({ x: ret.#x, y: ret.#y } = this);
    return ret;
  }
}

I don't think it's possible to destructure private fields.

@STRd6
Copy link
Contributor

STRd6 commented Feb 14, 2024

It looks like we'll need to do these manually. 😿

https://github.com/tc39/proposal-destructuring-private

@STRd6 STRd6 added the bug Something isn't working label Feb 16, 2024
@edemaine
Copy link
Collaborator

Until then, we could rewrite this as follows:

return.value{#x,#y} = @
↓↓↓
({ x: ret.#x, y: ret.#y } = { x: this.#x, y: this.#y });

Of course, if we had something like return.value{x,#x} = this, we'd have to do some more renaming...

Maybe worth implementing a general form of the TC39 proposal-destructuring-private spec at the same time.

@bbrk24
Copy link
Contributor Author

bbrk24 commented Feb 17, 2024

We could always use quoted keys:

({ "#x": ret.#x, "#y": ret.#y } = { "#x": this.#x, "#y": this.#y });

I suppose it's still possible to collide with non-private fields but I don't imagine ret{#x,"#x"} = @ to be a very common situation...

@STRd6
Copy link
Contributor

STRd6 commented Feb 17, 2024

I think we would instead do something like:

ret.#x = this.#x, ret.#y = this.#y;

Essentially pollyfilling without destructuring ourselves. We may need to consider what to do if there is an additional chained assignment but erroring in that case is probably fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants