I need to make one correction. I've just realised that you need to declare
i and j before the loops for some of my complaints to hold.
On Friday, 6 February 2015 12:25:34 UTC+1, David Higgins wrote:
>
> I have a use case I found interesting. I guess it's well known to some of
> you, but I never noticed it in the documentation and I find it
> counterintuitive, both as an experienced programmer and with respect to the
> typical scope rules of variables in Julia.
>
> I accidentally nested a loop within another loop using the same index
> variable for both loops. First of all, there are reasons sometimes you'd
> want to do this but I did it by accident as a typo. But the scope rules of
> variables for Julia, as applied to functions, state that you can read a
> variable value at a lower level in the hierarchy but you cannot write to it
> so I actually assumed that this also applied to loops (*first mistake*).
>
> Secondly, as you'll see if you execute the simplified example of what I
> actually did here:
> for i = 1:5
> for j = 1:5
> for i = 1:3
> print("tick ");
> end
> print(" $i\n");
> end
> print(" $j $i\n");
>
> Julia still knew to only run the outer loop 5 times, despite the fact that
> I was setting the variable bound to $i at level 2 to the value 3 at level 3
> in the nested hierarchy of loops. I suspect that I can see what's happening
> at the compiler/interpretter level, but I do think that this is confusing.
>
> We seem to have the worst of both worlds here in that we can access and
> change $i at an inner loop level and have the change be persistent, but yet
> we cannot change the behaviour of the outer loop (as would be the case in
> C).
>
> What do you guys think? Can this be clarified in the documentation, or
> should the actual behaviour of julia be modified?
>
> David.
>