-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Svelte 5: $state with computed properties + symbol (identifier) table #11358
Comments
In the case of the string it's not too much a problem as #11326 will allow to define these directly. But in the case of a symbol it's more problematic. Computed properties could be allowed for Motivation : this pattern would allow some interesting "private if you don't have the key" pattern This probably means the compiler should build an identifier table. (Tangent : would be awesome to expose it for preprocessors) |
If you need encapsulation, you can use a private field. class MyClass {
#state = $state(false);
onclick() {
console.log(this.#state);
}
}; |
That is not the same. TypeScript Real private properties do not conflict and cannot be accessed from outside the class. class A {
#state = false;
onclick() {
console.log(this.#state);
}
};
new A().onclick();
class B extends A {
#state = true;
onclick() {
console.log(this.#state);
}
};
new B().onclick(); |
I didn't know the different private field / modifier behaviors in details, and I think you're correct in my case this is what I want to do. Thank you @brunnerh Leaving this open for now while waiting for more feedback as it may still make sense in the bigger picture for Svelte to extend this js syntax / allow this pattern. Plus being able to access the identifiers table after compilation would allow awesome smart behaviors for preprocessors. But maybe this is a separate discussion and should go in another issue? |
What we can do is to create a stable private identifier that doesn't clash with other identifiers and then use a computed getter/setter pair, so that the compiled result would look something like this: class Foo {
#unique_id_123 = ..;
get [x]() { return get(this.#unique_id_123) }
set [x]() { .. }
} |
I also encountered an issue that seems similar: <script lang="ts">
class Test {
#value = $state(1);
get props() {
const that = this;
return {
get value() {
return that.#value;
}
};
}
}
const test = new Test();
</script>
<pre>{JSON.stringify(test.props, null, 2)}</pre> The result is: {
"value": {
"f": 0,
"reactions": null,
"v": 1,
"version": 0,
"inspect": {}
}
} |
@david-plugge, your issue is unrelated to this one, though the solution is the same: turn |
I spent this morning looking into seeing this was viable – the short story is that it's not because of the amount of complexity it adds to the compiler. If we were to allow computed public and private class property definitions, then that means we need to generate backing random private fields to store the state as @dummdidumm did above. That's fine and do-able for the class definitions as we can use the definition AST node as a key in our map to reference the random key we generate. However, this becomes very complicated on the usage sites – as So closing this as it's likely not possible and definitely not worth the complexity doing it statically. |
Describe the bug
... doesn't work.
Reproduction
https://svelte-5-preview.vercel.app/#H4sIAAAAAAAAE02PTWvDMAxA_4oQO6QQtntWB8bOPe1Y9eA5Smbm2CZSBiHkv484LfSkjyeepBV7H1iwua4Y7cjY4EfOWKMueS_kj4My1ihpntzeOYubfFYINg6GUIWwpUjqUhQFWcbvFMDAV0mq0_sT08nHAQwQujTmWbkjLLzMBCsCl-WzxLW09Xr4bmDgRdQqV70Nwod1x0X5hAkJCy08RRe8-wUD1cm0d-dxTgr8GtJQ6Y-Xx5aHdtvDdr_s_HY83FLEGsfU-d5zh41OM2-37R_7zjKHQQEAAA==
Logs
No response
System Info
Severity
would be great
The text was updated successfully, but these errors were encountered: