How one can create the reference to the field of the instantiated mutable struct?
I am just learning about this stuff, but I think this is impossible. I think the reason is that an isbits value like an Int or a Float64 can be “boxed” and therefore referenced, but an isbits value inside a struct cannot be “boxed”.
I think these are your options:
Use a pointer instead of a reference
Wrap your isbits value into a mutable struct by using e.g. Ref
Use properties to hide this wrapping
As shown in the documentation this could look like (I only implemented getproperty, you can define setproperty! yourself):
mutable struct A
v1_ref::Ref{Int}
v2_ref::Ref{Float64}
end
A(v1::Int, v2::Float64) = A(Ref{Int}(v1), Ref{Float64}(v2))
Base.propertynames(::A, private::Bool=false) = private ? (:v1, :v2, :v1_ref, :v2_ref) : (:v1, :v2)
function Base.getproperty(a::A, s::Symbol)
s === :v1 && return getfield(a, :v1_ref)[]
s === :v2 && return getfield(a, :v2_ref)[]
return getfield(a, s)
end