-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
potentially confusing behavior in cartesian loops #330
Comments
thought this might be a little clearer (if I understand the bug/unexpected behavior correctly)
|
I don't see what else this could do. |
@vtjnash: That's actually what it's supposed to do. It's a shorthand for a pair of nested for loops. @JeffBezanson: I guess why I found it surprising is that we're used to having loop variables reset on each iteration, regardless of what we do with them. In this case, however,
|
My worry is that somebody might rewrite one form to the other, thinking it is just a syntax shortcut. So the behavior shouldn't change. |
Let's just leave this behavior as-is and close this issue. |
This hit me today, I was doing something like this in image processing:
The behavior of the cartesian loop syntax was very confusing. |
This was closed in 2012, but in the meantime, the |
Yes, that's a good point. Either option seems ok to me; I'll sleep on it. Would be interesting to see what others think. |
I can see it either way, but I still think my original example is a bit of a gotcha. |
The comprehensions seem to suggest a change:
|
I do think there's a solid argument to be made there:
Therefore, we should change this. We can argue with each point, but if we accept both premises, that seems to be a solid argument. |
Do you mean changing the comprehensions with 2
So there is also an argument to be made to keep this double equivalence between for loops and comprehensions (by "double" I mean "nested for loop" <=> "nested for in comprehension" on one hand, and "cartesian for loops" <=> "cartesian comprehension" on the other hand). |
Adding triage here, as no definitive conclusion was reached since last discussion in September. |
That's a different issue... the behavior shown here does not happen anymore: julia> for x = Int64(2)^53-2:Int64(2)^53+5, y=1:2
println(x)
x = UInt64(x)
end
9007199254740990
9007199254740990
9007199254740991
9007199254740991
9007199254740992
9007199254740992
9007199254740993
9007199254740993
9007199254740994
9007199254740994
9007199254740995
9007199254740995
9007199254740996
9007199254740996
9007199254740997
9007199254740997 |
@rfourquet, if there's some other issue here, please open a new issue. |
The behavior shown here has been fixed when messing with the outer loop only, but still happens when messing with both loop indices:
Expected (
|
But that is only because julia> for x = Int64(2)^53-2:Int64(2)^53+5, y=1:2
println(typeof(x))
x = UInt64(x)
end
Int64
UInt64
Int64
UInt64
Int64
UInt64
Int64
UInt64
Int64
UInt64
Int64
UInt64
Int64
UInt64
Int64
UInt64 |
Ah, I see. Well, I could have sworn this was fixed. |
Puzzler: if I write
does |
A related issue is that these loops are not really "cartesian"; a loop iterator can depend on an outer variable, as in |
This currently matches what comprehensions do: julia> [(t=(i,j); i=nothing; t) for i = 1:3 for j = 1:i]
6-element Array{Tuple{Any,Int64},1}:
(1, 1)
(2, 1)
(nothing, 2)
(3, 1)
(nothing, 2)
(nothing, 3) where as it could mean this: julia> [let i=i, j=j; (t=(i,j); i=nothing; t); end for i = 1:3 for j = 1:i]
6-element Array{Tuple{Int64,Int64},1}:
(1, 1)
(2, 1)
(2, 2)
(3, 1)
(3, 2)
(3, 3) |
OK, decided we need to change both, due to this correspondence. |
Triage has badgered @JeffBezanson into agreeing to change this for both compound for loops and chained comprehensions. |
* Add file patterns to config.json file * Apply suggestions from code review Co-authored-by: Sascha Mann <[email protected]>
Not sure if this is a bug, but it did surprise me:
The text was updated successfully, but these errors were encountered: