set_dict=Dict{String, Any}("log_level" => 1.0, "area" => 20.0, "solver" => "IDA")
mutable struct Settings
log_level::Int64
area::Float64
solver::String
end
function set_struct(set_dict)
set = Settings(0, 0, "")
for (key, value) in set_dict
print(key, ": "); println(value)
# set field of the struct with the name key to the corresponding value
end
set
end
set_struct(set_dict)
But how can I access the fields of a struct using a string variable?
The dictionary will usually have less entries than the struct has fields.
set_dict=Dict{String, Any}("log_level" => 1.0, "area" => 20.0, "solver" => "IDA")
mutable struct Settings
log_level::Int64
area::Float64
solver::String
end
function set_struct(set_dict)
set = Settings(0, 0, "")
for (key, value) in set_dict
# set field of the struct with the name key to the corresponding value
setproperty!(set, Symbol(key), value)
end
set
end
set_struct(set_dict)