-
Notifications
You must be signed in to change notification settings - Fork 81
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
Switch conditional block #49
base: master
Are you sure you want to change the base?
Conversation
This reverts commit 5913b2f.
Added 0000-switch-block.md
Previous discussion concerning this: sveltejs/svelte#530 About the motivation, at least for me runtime performance is a non-concern in terms of switch vs if/else, it only seems to be relevant in micro-benchmarks, or in situations where there's a disproportionate amount of switch cases. The real win for me comes in the form of readability, and to some extent writability. Given that there exists significant precedent for switch/match statements in most popular programming language families, it's a common enough idiom that even a relatively junior developer would have been exposed to it, there is no surprise factor involved (which often comes with this kind of syntactic-sugar proposals). |
Lower expectations for speed improvements
I have updated the RFC to lower expectations of speed improvements as you suggested, although I did also write that it may improve performance on lower end hardware. |
As I have already commented on sveltejs/svelte#530 the argument that there should be less things for people to learn, can be applied to any language that supports |
I like this, I also think the syntax looks good! |
Quick question which syntax does everyone prefer (I prefer this one)
or (I don't like this one as much but it's more explicit)
Thumbs up for top, and thumbs down for bottom. |
I vote for the first one; even though the second one is as good. |
I'm more familiar with #2. Do any other programming languages use #1? I'm inclined to vote for #1, since it's less to write, but I anticipate one day cursing a block of broken code because I normally write switch blocks in the format of #2 . Maybe the mental context tool will be to remember that I'm writing template code, not JS. |
I don't know any other programming languages that use 1 but it would be cleaner, 2 matches the typical implementation more, but maybe it could be both ways somehow. It would just describe 2 in the tutorials, but mention that 1 is also an option, as well as not having a default at all by not puting anything in the default spot for 1 or 2 (with 2 you wouldn't add |
When I have time I think I will start working on making this new feature, but I'm not very familiar with the svelte source code so it may take me a while or I may just not be able to do it. |
I'd wait for a go-ahead from some maintainer before you start building it |
Ok I will wait then, but if it doesn't go through I may just make a fork for small personal projects where it won't matter as much like maybe a 1 page blog or something. |
I would stick with the coding style know by most developers. I would argue cleaner syntax isn't helpful if it is more error prone.
I think coding the Svelte compiler to support both 1 & 2 would introduce extra complexity to the source. But this is me speaking with no knowledge of how the compiler is written. A grammatical analogyTo me, it makes sense for the Imagine an employee instruction manual for how to make a sandwich for a customer How to make a sandwich To me, it's easier to read/comprehend the instructions with the default behavior placed at the bottom of the instructions. |
In the alternatives sections, is it worth discussing pattern matching as an alternative to copying JS's switch? Svelte could offer more powerful semantics than |
I tried it already, but couldn't do that because of my lack of knowledge of the source code. Doesn't seem to be so hard for the developers, though. |
I went back and forth a few times on the syntax. I really like the cleanliness of the first example, but feel that keeping things familiar makes the second one the winner. |
Svelte makes several bold changes - with |
At first glance this doesn't seem like a good idea. What value would it add? Svelte would have 2 ways to do the same thing, which (in principle) is not good. But after reflecting a bit more, a Some thoughts:
|
Concerning the comparison, I'd say stick with existing JS semantics concerning Regarding fall-through, I think not having any fall-through is a good compromise to avoid excessive syntax for the most common use-case (not having fall-through). This also eliminates the need for an explicit If fall-through is desired, I would go with something like Zig's switch statement, and use comma-separated values: {#switch (condition)}
<Tags that show if no cases are met>
{:case (expression1)}
<Tags that show if condition == expression1 />
{:case (expression2), (expression2FallThrough), ...}
<Tags that show if condition == expression2 />
{/switch} {#switch (condition)}
{:case (expression1)}
<Tags that show if condition == expression1 />
{:case (expression2), (expression2FallThrough), ...}
<Tags that show if condition == expression2 />
{:default}
<Tags that show if no cases are met>
{/switch} This also ties in with the proposal for a more powerful |
There will be no |
Also this part of svelte is a markup langauge, not a programming language. I do see some merit to this with adding an {#switch (condition)}
<Tags that show if no cases are met>
{:case (expression1)}
<Tags that show if condition == expression1 />
{#if (condition 2)}
{:break} <!-- This could also have (condition 2) integrated right into it -->
{/if}
<Tags that show if condition == expression1 && condition2 is met />
{:case (expression2), (expression2FallThrough), ...}
<Tags that show if condition == expression2 />
{/switch} but then again you could just use the |
Maybe this is what Rich Harris is worried about... simply invert the condition in the |
I actually see fall-through as a key feature for a new {#if variable === 'a'}
<ComponentA />
{:else if variable === 'b' || variable === 'c'}
<ComponentB />
{:else if variable === 'd'}
<ComponentD />
{:else}
<ComponentZ />
{/if} vs {#switch variable}
{:case 'a'}
<ComponentA />
{:case 'b'}
{:case 'c'}
<ComponentB />
{:case 'd'}
<ComponentD />
{:else}
<ComponentZ />
{/if} Yes, the But going this route, we might as well consider ideas from the pattern-matching proposal, as others have mentioned: https://github.com/tc39/proposal-pattern-matching <script>
let user = { status: 'pending' }
</script>
{#match res}
{:when { status: 'pending' }}
<Pending />
{:when { status: 'approved' }}
<Approved />
{:when { status: 'rejected' }}
<Rejected />
{:else}
<Error />
{/match} The C# language recently added a bunch of new features for pattern matching. Might be used as reference / inspiration too: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/patterns |
Too verbose for me. A comma separated list is much better, nicer and cleaner. Also |
One thing to consider, for the first switch syntax:
It mirrors the
|
Using a
And placing the default case immediately after |
There is no |
Can maintainers comment on this RFC? There are some people here that think it is very nice to have this feature. Also, it seems that the syntax is (almost) agreed on. Will it be implemented? Should we try to implement it ourselves and make a PR? |
Just wanted to chime in strongly in favour of the simple switch/case/default syntax:
This matches the JS My personal use case: I use Prismic as my CMS for all Svelte sites, and their 'slices' use conditionals based on a |
"everyone knows switch-cases!" Maybe we should also look into some similar mechanisms (match?), I don't really think we need that much writing. |
At first I was strongly in favor of the syntax matching the On the topic of
As a matter of fact, Golang In general, Regarding implementation difficulty and the learning curve, I think {#match animalType}
{:when "dog" | "cat"}
<h2>It's a mammal!</h2>
{:when "snake"}
<h2>It's a reptile!</h2>
{:default}
<h2>I don't know what that is!</h2>
{/match} Maybe it's best to wait for the JS pattern matching proposal to make headway. :/
|
This is pain. |
Any progress on this? Many people really want it (me included). What is the decision process on this kind of RFCs? Is it on some ToDo list somewhere, or we should provide a PR? |
I think it's about time for this to be implemented, most people agree with this feature, syntax is mostly set, and user case is well defined as plenty of us showed. I would love this for a far more cleaner component syntax. |
It does not seem like people agree on syntax at all. The RFC kind of hit a slump months ago and hasn't gotten much attention since then it seems. I for one, just pray that the end result plays nicely with TypeScript discriminated unions and control flow analysis, which is really the only way to do type-safe variant matching in TypeScript right now. That feature is absolutely critical for components which can be in several different states and have totally unrelated data (different types of variables) depending on what state the component is currently in. |
Two years have passed since the last comment. I understand most people agree this would be nice to have but there seems to be no agreement on how to implement it. What would need to happen to get this unstuck? |
this really should be a thing |
Rendered