8000 [Feature Request] Pattern Matching Operator · Issue #6496 · sveltejs/svelte · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
[Feature Request] Pattern Matching Operator #6496
Closed
@Chaostheorie

Description

@Chaostheorie

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0