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

Cannot use with #privateProperty #23

Open
lougreenwood opened this issue Nov 30, 2020 · 2 comments
Open

Cannot use with #privateProperty #23

lougreenwood opened this issue Nov 30, 2020 · 2 comments

Comments

@lougreenwood
Copy link

Previously discussed on Discord - https://discord.com/channels/480462759797063690/608346628163633192/781996880547938344

TLDR;

Because the specification for JS private class properties does not support dynamic lookup, macro decorators such as @reads() etc are not able to work with a string path passed to the decorator, since the decorator can't dynamically pick the string path from the this context - see here.

@lougreenwood
Copy link
Author

lougreenwood commented Nov 30, 2020

Would it be (technically) possible & desirable to allow passing in the class property itself, e.g:

@empty(this.#somePrivateThing) #thingExists;

I'm guessing not, since passing the path as a string allows the decorator getter to be lazy?

@pzuraq
Copy link
Owner

pzuraq commented Nov 30, 2020

So the decorator executes when the class is defined, not when the class is initialized. This means that this in your example code is bound to the external scope, not the class itself. In order to access the private field, you would need to be passed a function which can access the value instead, which could then be called with the class instance as the backing value:

@empty(function() { return this.#somePrivateThing }) #thingExists;

This function would also need to have access to the private name, which is not a normal thing that can happen. It would need to be included in the spec for decorators I believe. This is a good thing to bring up in that proposal, maybe you could open an issue on the decorators repo? https://github.com/tc39/proposal-decorators

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants