Skip to content

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

Closed
@JeffreySarnoff

Description

@JeffreySarnoff

[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)

Metadata

Metadata

Assignees

Labels

breakingThis change will break codeneeds decisionA decision on this change is needed

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions