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

[Feature Request] Pattern Matching Operator #6496

Closed
Chaostheorie opened this issue Jul 5, 2021 · 1 comment
Closed

[Feature Request] Pattern Matching Operator #6496

Chaostheorie opened this issue Jul 5, 2021 · 1 comment

Comments

@Chaostheorie
Copy link

Chaostheorie commented Jul 5, 2021

Describe the problem

I've recently began working with svelte for one of my projects that builds on top of enums for managing different variations of data. While enums (TS) are awesome for working with i.e. different types of shapes for rendering, it seems to be very cumbersome to work with them in svelte files since only if is available for matching them. To tackle this and other related problems, this request is for the introduction of the case (or match, switch … name is not really relevant) operator in svelte templates.

I have searched through the issues and haven't found a related one, but I'm sorry if this is a duplicate.

Describe the proposed solution

Add the case operator, with (would be fancy) optional enforcing of exhaustive matching.

An example usage:

<script type="ts">
// a simple enum
export enum Shape {
    Cat,
    Bunny,
    Dog,
    Plan,
    Car,
    Circle,
}

export let shape: Shape;
export let name: string;
</script>

{#case shape}
        // first declaration acts as 'otherwise' to ensure an exhaustive pattern
	<p>Animal is hiding!</p>
{:case Shape.Cat}
	<p>=^._.^=</p>
{:case Shape.Dog}
	<p>▼・ᴥ・▼</p>
{/case}


{#case name}
	<p>Welcome {name}!</p>
{:case "John"}
	<p>Welcome back {name}!</p>
{/case}

Alternatives considered

Simple matching with if is the current solution, but I've also considered using an auxiliary function to do the matching for me and return the right component.

Importance

would make my life easier

Edit 1: Changed title to more concise description

@Chaostheorie Chaostheorie changed the title [Feature Request] Case operator (Pattern Matching) [Feature Request] Pattern Matching Operator Jul 5, 2021
@dummdidumm
Copy link
Member

There's an open RFC for this where you can weigh in if you want: sveltejs/rfcs#49
Closing as a duplicate of that.

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

2 participants