-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
feat: allow $state
in return statements
#15589
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
base: main
Are you sure you want to change the base?
Conversation
🦋 Changeset detectedLatest commit: 8f71584 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes
I'm not sure if this should also apply to implicit returns in arrow functions, does anyone have any input on that? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it would be good to have a test, which would also help illuminate what is being supported
I'll add the test in a bit. This PR allows you to return a function fn() {
//...
return $state(obj);
} In this example, |
This is cool but I fail to see what use cases require you to directly return function reactive() {
const value = $state(...);
// Mutations/Side effects here
return value;
} I struggle to see where you would directly return |
It greatly simplifies the code. There is no reason not to push this PR. export const rune = <T>(initialValue: T) => {
const _rune = $state({ value: initialValue });
return _rune;
}; Why have that when I can simply have: export const rune = <T>(initialValue: T) => $state({ value: initialValue }); This is just the basic case, but it would definitely help get rid of pointless boilerplate like declaring an unnecessary variable. J |
@Hugos68 points is that it's very rare that you need to declare state and just return it. The reason it was initially prevented is because doing return $state(0); Is absolutely wrong |
We are using a compiler here. You didn't quote any specific reason other than just saying "wrong." How about we constructively consider how we can make this work instead of how we can't. If there is a bad use case, then we handle that separately. Thanks, J |
I might add a runtime warning if you don't pass a proxyable value to a |
It doesn't work for the same reason doing this let x = $state(0);
return x; Doesn't work: it was a design decision to be explicit about state crossing boundaries that was made for various reasons. Now: returning Also as I've commented I agree with hugo that is very rare that you need to just create a variable and return it without actually acting on it and at that point you need the reference. |
@paoloricciuti But that does work in a function. See original post #14316 Also, rare is relative until there are more Svelte 5 patterns. As of now, no AI can't even handle Svelte 5 code generation. |
It's not about svelte 5 patterns...there's little to no reason to do so in non example code. I'm not completely opposed to the feature request, but I don't think we should blindly make this decision |
What is non-example code? I gave a perfect example. I don't see how solving a problem is "blind." People will expect this Svelte code to compile. Are there specific examples or reasons that you or anyone else would like to reference? If anyone is worried about side effects, let's tackle them or push this to a test branch. |
Adding a new syntax for a problem that is not there 99% of the times is something to consider because if this new syntax introduces some "side effects" that are annoying to deal with you are adding maintenance burden for an edge case. Merging blindly just means merging for the sake of it, as I've said it was initially prevented because of a design choice, before re-evaluating that design choice we should think of all the possibilities. That's it. I don't have any specific examples right now, I'm just saying we should think if there are any. |
I think we are saying the same thing. That being said, sharing a simple state like that between components is not that rare IMO. I don't think anyone is coming up with specific issues so I'm not sure how long this should sit without comments? Isn't that the purpose of test branches / versions? |
This allows
$state
calls to be returned from a function, returning a proxy (if the argument can be proxied). I don't know if we should warn on arguments that aren't proxiable, I'm leaning towards 'no' since that would probably be unnecessary.Closes #14316
Before submitting the PR, please make sure you do the following
feat:
,fix:
,chore:
, ordocs:
.packages/svelte/src
, add a changeset (npx changeset
).Tests and linting
pnpm test
and lint the project withpnpm lint