Hi, first discourse topic/question ever, please do tell if this is the wrong place for this type of question. (I’m also new to Julia, so this might be trivial.)
I have a function, with a couple of keyword arguments.
(Disclaimer: I like keyword arguments to define broadly the environment/domain of my problem, and I prefer them to default arguments, because I can call and change just some of them by name).
Anyway, I thought I’d pull default values for kw args from some kind of object/structure, but also be able to change that structure at times (with a set function; a.k.a. change the “environment”), so object had to be a mutable, so I used a mutable struct:
mutable struct Parameters{T}
ao::T
bo::T
end
instantiated:
params = Parameters{Float64}(1.23, 8.77)
defined the function:
f(x; a = params.ao, b = params.bo) = ...
also defined a function to change the params:
function set(; new_ao = params.ao, new_bo = params.bo)
params.ao = new_ao
params.bo = new_bo
end
This, of course, works, but the problem is that mutable structs are apparently slow.
If instead of a struct I use a const array:
const params = [1.23, 8.77]
and define the function as: f(x; a = params[1], b = params[2]) = ...
(and also redefine the ‘set’ function) execution is 10x faster!
This is a hack, though: in the new set function I’m allowed to do params[1] = new_ao, although the params array is const! But in some future version of Julia, const arrays will presumably be really immutable.
So, is there an obvious alternative I’m missing? some elegant and more future-proof way to do what I want, and still have fast execution times?