In my program, I have two types of structs, one with a very longvector, and another that holds a view of a sub-part of the vector of another, reshaped to a matrix.
In principle this should work:
function reshapeView(vec, start, length, width)
return reshape((@view vec[start:(start + width*length - 1)]), length, width)
end
struct A
vec
end
struct B
mat
end
a = A(rand(100))
b = B(reshapeView(a.vec, 3, 3, 3))
Now b holds a view to 9 elements of a.vec, starting at index 3, reshaped to a 3x3 matrix as expected. If I change something in a.vec, it is reflected in b.mat. However, if I now define
struct C{T,N}
arr::Array{T,N}
end
C(vec::Vector{T}) where T = C{T,2}(reshapeView(vec, 3, 3, 3))
c = C(a.vec)
suddenly c holds a copy of the elements, instead of a view. Where am I implicitly copying?