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

Static support for $state in .svelte.ts #14804

Open
Bilge opened this issue Dec 22, 2024 · 8 comments
Open

Static support for $state in .svelte.ts #14804

Bilge opened this issue Dec 22, 2024 · 8 comments

Comments

@Bilge
Copy link

Bilge commented Dec 22, 2024

Describe the problem

I have a class that is entirely static (only static properties and methods). I cannot declare a $state property, either with a field initializer or in a static initialization block. In both cases, Svelte complains that:

_Error: CompileError: state_invalid_placement: $state(...) can only be used as a variable declaration initializer or a class field

Of course, this is a class field, it's just that it's a static field, which seems to be unsupported for some reason.

Describe the proposed solution

Support static class fields.

Importance

would make my life easier

@brunnerh
Copy link
Member

brunnerh commented Dec 22, 2024

Duplicate of:

Maybe this should be reconsidered? It seems quite inconsistent to not have this.

@Bilge
Copy link
Author

Bilge commented Dec 22, 2024

Certainly, "My inclination" seems fairly hand-wavery. The implication of this is that merely upgrading from v4 -> v5 requires any (external) static class interfacing with any Svelte component must be changed to be an instance class, which is wrong if the class is intended to be single-instance only.

@7nik
Copy link

7nik commented Dec 22, 2024

What is the point of defining it as a class, then? Why not just define is as an object with methods? Then you can wrap it into $state().

Also, a single instance can be created in IIFE-like way.

const instance = new (class MyClass {
  foo = $state(42);
  bar() {
    return this.foo;
  }
})();

@Bilge
Copy link
Author

Bilge commented Dec 22, 2024

I like classes. Objects cannot have private properties. I also don't like a component framework forcing me to structure my (external) application in a particular way.

@Leonidaz
Copy link

Leonidaz commented Dec 23, 2024

I couldn't tell from the previous tickets why defining signals as static members were disallowed.

But for private class members Svelte no longer generates getters and setters (only for public members). So, I wonder if at least private static members can now be allowed?

@ThePaSch
Copy link

ThePaSch commented Dec 23, 2024

Also, a single instance can be created in IIFE-like way.

const instance = new (class MyClass {
  foo = $state(42);
  bar() {
    return this.foo;
  }
})();

This is usually a very bad idea, unless you're okay with a 95% drop in performance; and therefore not a viable alternative.

@7nik
Copy link

7nik commented Dec 23, 2024

This is usually a very bad idea, unless you're okay with a 95% drop in performance; and therefore not a viable alternative.

Of course, it is slower because it also creates a new prototype chain for each instance. But it doesn't matter in the case of singleton.

@dummdidumm
Copy link
Member

Leaving this open to collect use cases since this indeed seems to come up from time to time

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

No branches or pull requests

6 participants