title: Julia: changing mutable struct attribute of choice in function
body: <p>I am creating a simple stochastic optimisation function in Julia and am using a mutable struct to store the parameter values.  Basically, I change a parameter in the mutable struct, pass these parameters through the performance function and update the parameter based on the performance.</p>
  
  <p>I have a version that works when I manually specify which parameters I would like to change.  However, I would like to generalise this so I can choose to change any given parameter.  For example, denoting the mutable struct of parameters as "args", and I am changing parameter "P", the key section of my current code looks like this:</p>
  
  <pre><code>args.P = P_new
  loss_new = performance_function(args)
  if loss_new &lt; loss_old # performance better, keep the new value
      loss_old = loss_new
  else # performance worse, go back to old value
      args.P = P_old
  end
  </code></pre>
  
  <p>I would like to generalise this into a function:</p>
  
  <pre><code>update_parameter(args,P,performance_function)
  </code></pre>
  
  <p>so that I can use any set of args, change any parameter P in args, and be able to use an arbitrary performance function.  To illustrate if args is defined as being of type Parameter,</p>
  
  <pre><code>mutable struct Parameter
      a::Float64
      b::Float64
  end
  
  args = Parameter(1,2)
  </code></pre>
  
  <p>Then I would like to update a then b using the same function,</p>
  
  <pre><code>update_parameter(args,a,performance_function)
  update_parameter(args,b,performance_function)
  </code></pre>
  
  <p>I can't work out how this can be done.  Is there some clever way using dictionaries? Or a better way that satisfies what I want avoiding the direct manipulation of mutable structs altogether? Thanks</p>