TIL: loop variables can be an element of an array, dict, etc

Been using Julia for several years and only just realised that you can use elements of an array, dict, etc as a loop variable. Eg

julia> v = zeros(Int, 3); for v[1] ∈ 0:3, v[2] ∈ v[1]:3, v[3] ∈ v[2]:3
         println(v)
       end
[0, 0, 0]
[0, 0, 1]
[0, 0, 2]
[0, 0, 3]
[0, 1, 1]
[0, 1, 2]
[0, 1, 3]
[0, 2, 2]
[0, 2, 3]
[0, 3, 3]
[1, 1, 1]
[1, 1, 2]
[1, 1, 3]
[1, 2, 2]
[1, 2, 3]
[1, 3, 3]
[2, 2, 2]
[2, 2, 3]
[2, 3, 3]
[3, 3, 3]

Not a big deal but kinda neat

3 Likes

Also works for properties (dots, setproperty! shows up in Meta.@lower). This isn’t documented so I’m tentatively considering this an unstable lowering quirk.

1 Like