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

Allow omitting the as clause in each statement #8348

Closed
hgiesel opened this issue Mar 3, 2023 · 6 comments · Fixed by #14396
Closed

Allow omitting the as clause in each statement #8348

hgiesel opened this issue Mar 3, 2023 · 6 comments · Fixed by #14396
Assignees
Milestone

Comments

@hgiesel
Copy link
Contributor

hgiesel commented Mar 3, 2023

Describe the problem

It would be nice if we could forego the as part of an #each statement, if we don't need it.

Currently we have to write it like this.

This is probably a good first issue

Describe the proposed solution

Instead we could just write {#each param}{/each}

Alternatives considered

Just assign to _, as I have done above.

Importance

nice to have

@hgiesel hgiesel changed the title Allow omitting the as clause in `each statement Allow omitting the as clause in each statement Mar 3, 2023
@Prinzhorn
Copy link
Contributor

Prinzhorn commented Mar 3, 2023

Can you post an actual example where this would be needed? As this is much simpler written as:

<div style:width="{indent * 10}px" />

Even if there is such an example, I don't think it warrants the additional syntax. This is also a duplicate of #2968 because you just want to iterate N times. Such a #range block could arguably have ...as... be optional.

@hgiesel
Copy link
Contributor Author

hgiesel commented Mar 3, 2023

Closing in favor of #2968

@oscarhermoso
Copy link

oscarhermoso commented Jul 27, 2023

@hgiesel Consider reopening this?

Now that Svelte 4 supports iterators, this could be the syntax for a range expression

{#each range(1, 5)}
  ...
{/each}

@hgiesel
Copy link
Contributor Author

hgiesel commented Jul 27, 2023

@oscarhermoso Sure! With the support for iterators, this makes a whole lot more sense than a new keyword

@hgiesel hgiesel reopened this Jul 27, 2023
@Rich-Harris Rich-Harris added this to the 5.0 milestone Apr 1, 2024
@Rich-Harris
Copy link
Member

I think I like this idea. I'd want to be able to include the index in {#each Array(n), i}.

It's a small change, but it feels less messy and more intentional. It seems like there's a potential ambiguity with TypeScript (if there's only one as, could it mean a typecast?) but since you can only access the item if it's named, it's totally fine to stipulate that if there's only one as it's not a typecast.

Reasons not to do this:

  • it's more syntax to learn
  • it's possible that we'd want to overhaul each blocks in future, and this could feel churny

Having said that, #2968 is an extraordinarily popular feature request, and this seems like a better solution to that problem.

@dummdidumm dummdidumm modified the milestones: 5.0, 5.x May 13, 2024
dummdidumm added a commit that referenced this issue Nov 13, 2024
@dummdidumm dummdidumm mentioned this issue Nov 21, 2024
6 tasks
@Rich-Harris
Copy link
Member

(closed by accident)

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

Successfully merging a pull request may close this issue.

5 participants