How to make a struct that contains a reference to other memory?

foo[1:2] makes a copy of the elements.
What you want to use here is bar = Bar(2, [ Ref(foos, i) for i in 1:2 ]). See the doc string of Ref.

Also note that

julia> isabstracttype(Ref{Foo})
true

which will make iterating over this vector slow.
You should instead use Vector{Base.RefValue{Foo}}.


Yes, they can be used that way.

But if you are dealing with arrays, I would recommend you to work with view and SubArrays instead.

E.g.

julia> struct Bar{V<:AbstractVector{<:Foo}}
           l::Int
           o::V
       end

julia> bar = Bar(2, view(foos, 1:2))
4 Likes