-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
[Feature Request Runes] Change $derived() to accept a function with a return value #9250
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
Comments
I get where you are coming from, but runes already add so much noise to components that I personally would rather keep it as is. |
|
@Conduitry, I'm surprised why this feature request was shut down so quickly, without much consideration. So the justification for keeping this unnatural and less flexible syntax is that even though I would appreciate it if my proposal could at least be given some consideration, rather than just being abruptly closed. With a returned function as the argument, multiline code becomes incredibly easy and idiomatic to already do in JavaScript. let price = $state(23)
let taxRate = $state(0.06)
const totalPrice = $derived(() => {
const tax = price * taxRate
return price + tax
}) |
I actually think this is a great argument to why it should also accept a function. Otherwise you would have to use another state and effect to update that state, if I’m understanding Svelte 5 correctly. While I understand this is how you would have done it in Svelte 4, I think that would be one of the many benefits of moving to signals. I know you can do |
Tradeoff: why not a separate $computed rune that accepts a function? I'm more on the function side because it doesn't seem right to be doing this instead, which adds a lot of noise the moment you go beyond simple derivations
I suppose Svelte could have some optimizations here to prevent generating this code:
|
@intrnl You can inline the function and call it directly, this adds a few parentheses but probably does not warrant adding a separate rune. const x = $derived((() => {
// ...
})()); |
@brunnerh, but the current // Single line
const area = $derived(() => width * height)
// Multi line
const area = $derived(() => {
return width * height
}) The previous example is much more consistent, predictable and idiomatic than the current // Single line
const area = $derived(width * height)
// Multi line
const area = $derived((() => {
return width * height
})()) |
Yeah, I think the added |
Uh oh!
There was an error while loading. Please reload this page.
Describe the problem
While
$derived()
is a welcomed Rune addition, I don't quite understand why it has a confusing syntax of:Multi line code ends up adding way too much noise too, as it requires an IIFE:
Describe the proposed solution
It would be much more idiomatic in JavaScript for it to accept a function with a return value, so:
This is also more flexible for multiline code:
Alternatives considered
There's no alternative in Runes that I'm aware of.
Importance
would make my life easier
The text was updated successfully, but these errors were encountered: