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

should for create a new binding for each iteration? #1379

Closed
JeffreySarnoff opened this issue Oct 14, 2012 · 2 comments
Closed

should for create a new binding for each iteration? #1379

JeffreySarnoff opened this issue Oct 14, 2012 · 2 comments
Assignees
Labels
breaking This change will break code needs decision A decision on this change is needed

Comments

@JeffreySarnoff
Copy link
Contributor

[post from Stefan regarding example below]

The key is that without a let the same i is captured by each closure, so the two closures actually do the same thing. With a let i, each iteration has a different i and the closures are thus different and each one when called will return the value of its own i, i.e. the corresponding iteration number. This is a very Lispy thing.

I have previously thought that it might be good if each iteration of a for loop implicitly had its own i, but that might adversely affect performance. I suspect, however, it would be easier to reason about, would possibly allow us to eliminate the let keyword, and might be more friendly to implicit parallelism since it breaks the dependency between different iterations.

[the example]

Does Julia intend that these two ways of assigning an anonymous function yield different results?

M=Array(Any,2)
for i in 1:2
M[i] = () -> i
end

M1, M2
(2, 2)

while

N=Array(Any,2)
N[1] = () -> 1
N[2] = () -> 2
N1, N2
(1,2)

@JeffBezanson
Copy link
Member

Yes, currently the intent is for for to mutate the loop variable. I would definitely consider having it (semantically) create a new binding for each iteration, which is more compatible with parallelization among other things.

@ghost ghost assigned JeffBezanson Nov 28, 2012
@JeffBezanson
Copy link
Member

Dup of #1571. Current thinking is that we will make this change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking This change will break code needs decision A decision on this change is needed
Projects
None yet
Development

No branches or pull requests

2 participants