julia> CartesianIndex(1,1) .+ [CartesianIndex(i,i) for i in 1:10]
ERROR: iteration is deliberately unsupported for CartesianIndex. Use `I` rather than `I...`, or use `Tuple(I)...`
Stacktrace:
[1] error(::String) at .\error.jl:33
[2] iterate(::CartesianIndex{2}) at .\multidimensional.jl:164
[3] copyto!(::Array{Int64,1}, ::CartesianIndex{2}) at .\abstractarray.jl:721
[4] _collect(::UnitRange{Int64}, ::CartesianIndex{2}, ::Base.HasEltype, ::Base.HasLength) at .\array.jl:609
[5] collect(::CartesianIndex{2}) at .\array.jl:603
[6] broadcastable(::CartesianIndex{2}) at .\broadcast.jl:665
[7] broadcasted(::Function, ::CartesianIndex{2}, ::Array{CartesianIndex{2},1}) at .\broadcast.jl:1235
[8] top-level scope at REPL[5]:1
(CartesianIndex(1,1),) .+ [CartesianIndex(i,i) for i in 1:10]
Wrapping it in a tuple indicates the first argument is to be broadcast. This is arguably a case where defensive programming gets in the way. If we were confident that no one would try to broadcast over the āelementsā of a CartesianIndex we could make what you tried work.
julia> [each for each in CartesianIndex(1,1)]
ERROR: iteration is deliberately unsupported for CartesianIndex. Use `I` rather than `I...`, or use `Tuple(I)...`
Stacktrace:
[1] error(::String) at .\error.jl:33
[2] iterate(::CartesianIndex{2}) at .\multidimensional.jl:164
[3] iterate at .\generator.jl:44 [inlined]
[4] collect(::Base.Generator{CartesianIndex{2},typeof(identity)}) at .\array.jl:665
[5] top-level scope at REPL[13]:1
julia> iterate(CartesianIndex(1,1))
ERROR: iteration is deliberately unsupported for CartesianIndex. Use `I` rather than `I...`, or use `Tuple(I)...`
Stacktrace:
[1] error(::String) at .\error.jl:33
[2] iterate(::CartesianIndex{2}) at .\multidimensional.jl:164
[3] top-level scope at REPL[14]:1
julia> for each in CartesianIndex(1,1) println(each) end
ERROR: iteration is deliberately unsupported for CartesianIndex. Use `I` rather than `I...`, or use `Tuple(I)...`
Stacktrace:
[1] error(::String) at .\error.jl:33
[2] iterate(::CartesianIndex{2}) at .\multidimensional.jl:164
[3] top-level scope at .\REPL[15]:1
Right. The bottom line is we want people to use CartesianIndex like a scalar; A[3, 4] should be identical in every respect with A[CartesianIndex(3, 4)]. That imposes certain rules with respect to broadcasting, including the fact that you canāt iterate over (3, 4) as if itās a list. But what we donāt (yet) do is trust that people wonāt try to iterate over those entries, so we look for that and try to throw a helpful error. However, in your case, that attempt to be helpful actually got in the way of the perfectly well-justified idea you had in the beginning.