Hello everyone,
Currently setfield! and setproperty! need two arguments to set fields.
setfield!(value, name::Symbol, x)
Assign x to a named field in value of composite type.
The value must be mutable and x must be a subtype of fieldtype(typeof(value), name). See also setproperty!
Since calling setfield!(v,n,x) or setproperty!(v,n,x) is like using it on a separated Pair{Symbol,Any}(n,x) with n::Symbol and x the value.
Should these methods be extended with Pair arguments ? It could be convenient when iterating on a dict for setting the fields of a structure
for p in dict_sym_val
setfield!(my_struct,p) # instead of setfield!(my_struct,p[1],p[2]) or setfield!(my_struct,p...)
end
And also maybe allow multiple fields like
setfield!(my_struct;dict_sym_val...)
:-1: I think having two ways to call this is too complex. If the loop is written as for (k,v) in dict; setfield!(x, k, v) it's not so bad.
Most helpful comment
:-1: I think having two ways to call this is too complex. If the loop is written as
for (k,v) in dict; setfield!(x, k, v)it's not so bad.