back

by tobr·8y ago·view on hn ↗
The problem with Svelte's computed properties is that it's not just an idiom, but a completely new language that happens to look like JS. It does things JS can't, and breaks very basic things like functional composition.

Why not simply use the "correct" ES6 syntax to do the same thing? It's almost identical:

  computed: {
    hours: ({time}) => time.getHours(),
    minutes: ({time}) => time.getMinutes(),
    seconds: ({time}) => time.getSeconds()
  }
This way, the compiler could simply optimise idiomatic cases where it's easy to see which data is depended on, without breaking the language.
2 comments
Interesting idea! I've raised an issue, thanks — https://github.com/sveltejs/svelte/issues/1069
At first, this looked like a bad idea but now can see the value but also see the ugly and less-friendly to newcomers angle.

Trouble is, coming from a Vue background (where computed props do not have to rely on a state item), the pre-requisite in Svelte to do so seemed at first a major PITA (then I saw the light/benefits and was actually easily able to re-work the Vue versions to Svelte).

Unless this is the only place Svelte goes slightly 'off-piste' in terms of JS then personally I'd leave as is, otherwise make the change