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

Range over slice/map with single var assignment should set the var to the index/key #158

Closed
sauerbraten opened this issue Jul 2, 2020 · 0 comments · Fixed by #159
Closed

Comments

@sauerbraten
Copy link
Collaborator

sauerbraten commented Jul 2, 2020

Currently, {{ range foo := someSlice }} will set both . and foo to the element value of the current iteration. To make this one-var syntax more useful, I would like range to set foo to the index of the element instead.

Iterating over a map with a one-variable assignment would set that var to the key of the current element.

Iterating over a channel with a one-variable assignment would set that var to the current element as before, since channel element values don't have a key or index.

{{ range foo, bar := someSlice }} would still set foo to the index, and bar to the element itself.

This is of course a breaking change, so definitely a major version bump.

@sauerbraten sauerbraten changed the title Range over slice with single var assignment should set the var to the index Range over slice/map with single var assignment should set the var to the index/key Jul 2, 2020
sauerbraten added a commit to sauerbraten/jet that referenced this issue Jul 2, 2020
…oses CloudyKit#158)

basically, if the ranger provides both a range value as well as an index, the one-var syntax sets that var to the index. if the ranger provides no index, one-var syntax will give you the value. this way, Jet's range now behaves like Go's range for maps, slices, arrays, and channels.
sauerbraten added a commit that referenced this issue Jul 3, 2020
…oses #158)

basically, if the ranger provides both a range value as well as an index, the one-var syntax sets that var to the index. if the ranger provides no index, one-var syntax will give you the value. this way, Jet's range now behaves like Go's range for maps, slices, arrays, and channels.
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.

1 participant