-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Switch statements [enhancement] #530
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 can see that being useful for e.g. routing. How exactly do you see the syntax working? This... {{#switch route}}
{{#case '/'}}
<Home/>
{{/case}}
{{#case '/settings'}}
<Settings/>
{{/case}}
{{#case '/about'}}
<About/>
{{/case}}
{{/switch}} ...is a bit noisier than the equivalent (which presumably it would be converted to internally): {{#if route === '/'}}
<Home/>
{{elseif route === '/settings'}}
<Settings/>
{{elseif route === '/about'}}
<About/>
{{/if}} Perhaps there's a more compact way of expressing the same thing? It also introduces a bit of ambiguity about fallthrough and defaults which you don't get with |
It's just the tidyness of one variable to map the states based on value.
Similar to switch functionality that I care about any kind of syntax would
work. I think it's just asking what color to paint the shed. Personally I
don't know. Probably just not switch as that may confuse people thinking
that the functionality is 1:1
…On 1/05/2017 12:39 AM, "Rich Harris" ***@***.***> wrote:
I can see that being useful for e.g. routing. How exactly do you see the
syntax working? This...
{{#switch route}}
{{#case '/'}}
<Home/>
{{/case}}
{{#case '/settings'}}
<Settings/>
{{/case}}
{{#case '/about'}}
<About/>
{{/case}}
{{/switch}}
...is a bit noisier than the equivalent (which presumably it would be
converted to internally):
{{#if route === '/'}}
<Home/>
{{elseif route === '/settings'}}
<Settings/>
{{elseif route === '/about'}}
<About/>
{{/if}}
Perhaps there's a more compact way of expressing the same thing?
It also introduces a bit of ambiguity about fallthrough and defaults which
you *don't* get with if. Any thoughts on how you would avoid that?
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#530 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AIiaJC78-UO57DBwZ6HEQckFP2a4tSzqks5r1IEJgaJpZM4NJmcE>
.
|
It's not just bikeshedding. Without a better syntax than the strawman above, it's just a less good way of doing something that already exists 😀. If it's not |
I think I'd come down in favor of no new syntax for this. It seems the main reason someone would want this would be to not have to recompute whatever it is they're comparing in each if/elseif. But then they should just be using computed properties. |
Okay fair. I think your example would be effective. The {{/case}} should
differentiate it enough to not expect fall through function.
However if elseif else does work. And maybe it would be less confusing to
have a semi consistent switch statement that isn't quite how a new user may
be used to.
…On 2/05/2017 2:59 AM, "Rich Harris" ***@***.***> wrote:
It's not just bikeshedding. Without a better syntax than the strawman
above, it's just a less good way of doing something that already exists 😀.
If it's *not* switch, then we veer into inventing our own language which
is dangerous territory — the less people have to learn, the better.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#530 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AIiaJH6MtOLoNiXrFWJu49z91kBw8eiqks5r1fNYgaJpZM4NJmcE>
.
|
Will close this, as I don't think there's sufficient value in adding new syntax |
@Rich-Harris any chance of revisiting this idea under the newer svelte 3 syntax? I was just thinking about this, something like this feels natural within the context of svelte's template syntax, and has familiar semantics and syntax comparable to a JavaScript switch statement (or in any C-like language, really). {#switch x}
{:case 5}
<p>{x} is 5</p>
{:case 10}
<p>{x} is 10</p>
{:default}
<p>{x} is not 5 or 10</p>
{/switch}
<!-- equivalent to something like: -->
{#if x === 10}
<p>{x} is 10</p>
{:else if x === 5}
<p>{x} is 5</p>
{:else}
<p>{x} is not 5 or 10</p>
{/if} It's not necessarily shorter, but it seems clearer and explicit about its switch/match semantics. I think not having any fall-through is a good compromise to avoid the complications with syntax mentioned in the previous responses. |
Hello, I think quite a few people like this enhancement proposed by @jpaquim and @mjadobson. Maybe we can reopen the ticket! TL;DR: bump |
I agree. I found this by looking for how to write switch statements in svelte. It is a lot more concise and readable.
Just providing another example below to demonstrate how much nicer looking it is:
Also, it turns out that performance of switch is better than that of if else. See: Here Edit: Mentioned performance. |
Bump! I'd love to see this implemented! It would be really useful as I much prefer to use a switch for many conditions for the same value. Please consider reopening this! |
For anyone who came here looking for a workaround, i came up with this simple trick that takes advantage of the fact that <div>
<!-- re-run every time `my_instance` is changed -->
{#key my_instance}
<!-- cache the result of the expression to `switch_value` -->
{#await some_expression_using(my_instance, and_data) then switch_value}
{#if 'case_1' === switch_value}
<h4>Case 1</h4>
{#else if 'case_2' === switch_value}
<h4>Case 2</h4>
{#else}
<h4>Default Case</h4>
{/if}
{/await}
{/key}
</div> |
@Rich-Harris , please, kindly re-open. This would be a nice feature to have. I'm really not a fan of Thank you. |
I just came into a need for this. |
Just for quick reference, in case this gets re-opened… Here's a transcript of the syntax that @Rich-Harris recently explored at Svelte Summit. It's basically the same as what's already been suggested by @jpaquim and @mikeoptics, with added consideration for empty defaults. Default {#switch thing}
something else
{:case "foo'}
f00
{:case 'bar'}
bar
{:case 'baz'}
baz
{/switch} No default {#switch thing case 'foo' }
f00
{: case 'bar'}
bar
{:case 'baz'}
baz
{/switch! |
Thanks for the link, I hope he goes with this @Rich-Harris / @Conduitry |
I would totally love if this would be implemented. Looks and feels more cleaner! |
This approach would be even cleaner, its not necessary to have # : or / because every block can be identified
To make it even more readable we can use the Liquid Approach. Its basically the same as Switch
|
When paired with Typescript + enums or union types, I think a switch statement can add a useful warnings. The Dart Lang has a missing_enum_constant_in_switch diagnostic message, that helps users handle all enum values. Missing a value? Get a warning. Here's a short video showing the dart behaviour, and a link to the dartpad code:
For example, I'd love to see Missing case clause for 'error' here: <script lang="ts">
export let status: 'loading' | 'ok' | 'error';
</script>
{#switch status case 'loading'}
Loading...
{:case 'ok'}
Awwww, yis
{/switch} |
It would be nice to have a {{#switch (VARIABLE-NAME) }} tag with complementary {{case (VALUE)}} tags. I would like to use it in a SPA rather than a number of {{if}} {{elseif}} tags.
The text was updated successfully, but these errors were encountered: