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

Change the for loop to create a new binding per iteration #1724

Open
hanche opened this issue Sep 20, 2023 · 1 comment
Open

Change the for loop to create a new binding per iteration #1724

hanche opened this issue Sep 20, 2023 · 1 comment

Comments

@hanche
Copy link
Contributor

hanche commented Sep 20, 2023

As mentioned in chat today, this could catch some by surprise:

⬥ for x [a b c] { put { constantly $x} } | each  {|f|  ($f) }
⮕ c
⮕ c
⮕ c

I suggest creating a new binding for the loop variable with each iteration of the loop, essentially turning for x $seq { … } into all $seq | each {|x| … }.

This is of course a backward incompatible change. In particular, some users might use the value of the loop variable after the loop is done, to see what was the last value handled. This would now have to be done manually.

Also, it is conceivable that this would be a performance problem with loops iterating a lot, if all those variable bindings now have to be handled by the garbage collector. One might consider having the compiler analyze the code and reuse the variable binding if it is not captured inside the loop.

@krader1961
Copy link
Contributor

This is effectively the same issue as golang/go#56010 for Go itself. And it seems clear that Go is going to change its loop var binding behavior since doing so is going to break very few programs and result in a big reduction in bugs. Elvish should make the same change.

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