Reference/Mutation Problem


#1

Hi guys

I am trying to do the following:

abstract type Operator end;

mutable struct Multiply <: Operator
 multiplier::Float64
end

mutable struct Incrementor <: Operator
   inc::Int
end 

mutable struct CompositeOp <:Operator
  mulr::Multiplier
  incr::Incrementor
end
  
c = CompositeOp(Multiply(2.2), Incrementor(3))

I want

c.multiplier
c.inc

to return 2.2 and 3 respectively and when i make an assignment to those the values in mulr and incr to change accordingly .how can i do something like this using setfield!, getfield and maps?
Or another solution is welcome.

Thanks


#2

It appears you want a type where you can change the binding of the fields. You should try mutable struct instead of struct.

See also https://docs.julialang.org/en/stable/manual/types/#Mutable-Composite-Types-1


#3

Sorry i forgot to put mutable keyword in front of structs…


#4

Anyone? Any Suggestions?


#5

In v0.6 you can make functions like change_multiplier(c,...). In v0.7 there’s getfield overloading.