This change is not trivial to do. Consider this, which must continue to work:
mutable struct A
el::Int
end
a = A(1)
b = [ a ]
b[1].el = 2
a.el == 2 # true
Evidently, trying to declare b
to have its elements stored in-line doesn’t work with the non-moving GC we have today; a
already exists before b
is ever allocated, so either the in-line semantics of b
breaks, the use of a
in b
copies (thus breaking the ==
test) or an error is thrown. Neither of these seem good.
That Julia stores mutable values as references is by design; Julia doesn’t have the reference semantics exposed to be manipulated/worked with by the user. There are undoubtly situations where treating a collection and the objects it contains as one entity for the purposes of memory management would be useful, but that’s not the semantics Vector
(or mutable objects for that matter) has today.
That being said, if you do want to have those kinds of semantics, you can somewhat get to them using Ref
(pointing it to an array like Ref([ a ], 1)
), which must explicitly be indexed in order to retrieve the stored (mutable) object.