Why are ref's in structs type unstable?

Ref{Int} is an abstract type. Ref(0) creates a Base.RefValue{Int}, which is a concrete subtype of Base.Ref{Int}:

julia> isconcretetype(Ref{Int})
false

julia> Ref{Int}(5)
Base.RefValue{Int64}(5)

julia> isconcretetype(Base.RefValue{Int64})
true

Therefore, the struct field should have the type Base.RefValue{Int} instead of Ref{Int} if you want type stability.

3 Likes