This does seem like an intentional design choice because reshape
ing Array
s does not use the wrapper ReshapedArray
. However, consider these counterexamples to the two points of your understanding:
- The shared data error is not thrown if you made views or forced a
ReshapedArray
, even with the much more disruptiveinsert!
. Theflags.how
protection might be limited toArray
s.
julia> x = zeros(2); y = reshape(x, 1, 2)
1×2 Matrix{Float64}:
0.0 0.0
julia> push!(x, 7.0)
ERROR: cannot resize array with shared data
julia> x = zeros(2); y = view(x, 1:1)
1-element view(::Vector{Float64}, 1:1) with eltype Float64:
0.0
julia> push!(x, 7.0); insert!(x, 1, 1.0); y # y is corrupted!
1-element view(::Vector{Float64}, 1:1) with eltype Float64:
1.0
julia> x = zeros(2); y = Base.ReshapedArray(x, (1,2), ())
1×2 reshape(::Vector{Float64}, 1, 2) with eltype Float64:
0.0 0.0
julia> push!(x, 7.0); insert!(x, 1, 1.0); y # y is corrupted!
1×2 reshape(::Vector{Float64}, 1, 2) with eltype Float64:
1.0 0.0
- When you
reshape
anArray
to the same shape, you just reuse the instance, the ultimate sharing. Of course,push!
does not reallocate. It could be a hassle to check whetherreshape
made a separate instance or not to keep the variables’ behavior consistent during mutations.
julia> x = zeros(2); y = reshape(x, 2);
julia> x === y
true
julia> push!(y, 7.0); x === y # x seems corrupted if you didn't check
true