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

Add a {#switch} block #13507

Closed
Ocean-OS opened this issue Oct 4, 2024 · 3 comments
Closed

Add a {#switch} block #13507

Ocean-OS opened this issue Oct 4, 2024 · 3 comments

Comments

@Ocean-OS
Copy link
Contributor

Ocean-OS commented Oct 4, 2024

Describe the problem

It is common to test a value to see what it is equal to, using multiple if/else blocks. However this can get tedious and repetitive quickly. Example:

{#if value === "Hi"}
Oh, hello!
{:else if value === "Bye"}
Goodbye!
{:else}
I don't understand...
{/if}

Describe the proposed solution

A {#switch } block would be useful. It would work like the JS switch block. Here's an example:

{#switch value break}
{:case "Hi"}
Oh, hello!
{:case "Bye"}
Goodbye!
{:default}
I don't understand...
{/switch}

The break part in the {#switch } block means that it breaks at the end of each {:case} block. If this is not in the switch block, a {:break} block can be used to break at the end of a case block. Here's a revision of the code above using {:break} blocks:

{#switch value}
{:case "Hi"}
Oh, hello!
{:break}
{:case "Bye"}
Goodbye!
{:break}
{:default}
I don't understand...
{/switch}

Importance

would make my life easier

@Conduitry
Copy link
Member

See sveltejs/rfcs#49. There has been a lot of discussion about this in the past, and it wasn't clear how to make the semantics of a switch statement work nicely with the block markup syntax.

@Conduitry Conduitry closed this as not planned Won't fix, can't repro, duplicate, stale Oct 4, 2024
@Devr-pro
Copy link
Contributor

See sveltejs/rfcs#49. There has been a lot of discussion about this in the past, and it wasn't clear how to make the semantics of a switch statement work nicely with the block markup syntax.

I think this issue should be reopened because the original rfc isn't active much, last discussion was couple of years ago, atleast with this issue there would be some attention on this

@samclaus
Copy link

samclaus commented Jan 23, 2025

@Devr-pro

I think this issue should be reopened because the original rfc isn't active much, last discussion was couple of years ago, atleast with this issue there would be some attention on this

Yeah there hasn't been a lot of discussion because no one has been able to offer an elegant path forward. Seriously, how much typing is this feature going to save you vs. how much complexity is it going to add to the Svelte codebase and documentation? People 20 years ago thought it was a great idea to have 2 forms of "null" in JavaScript and the negative repercussions of that terrible decision have been massive.

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

No branches or pull requests

4 participants