Multidimensional arrays as indices in setindex!: bug or feature?

Hello everyone,

I just found that one can use multidimensional arrays as indices within setindex!. They behave as 1d arrays, which is different from their behavior in getindex, thus the following assertion from the docs doesn’t hold:

If any index I_k is itself an array, then the right hand side X must also be an array with the same shape as the result of indexing A[I_1, I_2, ..., I_n] or a vector with the same number of elements.

Is this a bug or a feature? Indexing with multidimensional arrays is quite unintuitive, and one should just use 1d arrays if the behavior is identical anyway.

Examples:

julia> x = [ 1  2  3  4
             5  6  7  8
             9 10 11 12
            13 14 15 16]
4×4 Matrix{Int64}:
  1   2   3   4
  5   6   7   8
  9  10  11  12
 13  14  15  16

julia> y = zero(x); y[[1 2; 3 4], [1 2; 3 4]] = x; y
4×4 Matrix{Int64}:
  1   3   2   4
  9  11  10  12
  5   7   6   8
 13  15  14  16

julia> y = zero(x); y[[1, 3, 2, 4], [1, 3, 2, 4]] = x; y
4×4 Matrix{Int64}:
  1   3   2   4
  9  11  10  12
  5   7   6   8
 13  15  14  16

julia> y[[1 2; 3 4], [1 2; 3 4]]
2×2×2×2 Array{Int64, 4}:
[:, :, 1, 1] =
 1   9
 5  13

[:, :, 2, 1] =
 2  10
 6  14

[:, :, 1, 2] =
 3  11
 7  15

[:, :, 2, 2] =
 4  12
 8  16

2 Likes

The next line is also confusing to me

The value in location I_1[i_1], I_2[i_2], ..., I_n[i_n] of A is overwritten with the value X[I_1, I_2, ..., I_n], converting if necessary.

Just seems like if X has the same shape as A[I_1, I_2, ..., I_n], then it could have a different shape from A and should not be indexed like A is. X[I_1, I_2, ..., I_n] should have the same shape as A[I_1, I_2, ..., I_n][I_1, I_2, ..., I_n], which doesn’t seem to reach a scalar value.