For the following Julia code:
mutable struct MutationWeights
mutate_constant::Float64
optimize::Float64
end
const mutations = [fieldnames(MutationWeights)...]
@generated function MutationWeights(;
mutate_constant=0.048,
optimize=0.0,
)
return :(MutationWeights($(mutations...)))
end
"""Convert MutationWeights to a vector."""
@generated function Base.convert(
::Type{Vector}, weightings::MutationWeights
)::Vector{Float64}
fields = [:(weightings.$(mut)) for mut in mutations]
return :([$(fields...)])
end
I wrote the following C++ alternative:
struct MutationWeights {
float mutate_constant = 0.048;
float optimize = 0.0;
};
const auto mutations = std::vector<std::string>{"mutate_constant", "optimize"};
template <typename... Args>
MutationWeights createMutationWeights(Args&&... args) {
return {std::forward<Args>(args)...};
}
std::vector<double> convertMutationWeightsToVector(const MutationWeights& weightings) {
return {weightings.mutate_constant,
weightings.optimize};
}
Is this the most sensible way of doing it, or would you suggest something different?