Julia: allow setfield! | setproperty! to work with pairs

Created on 7 May 2019  路  1Comment  路  Source: JuliaLang/julia

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...)

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.

>All comments

:-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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

TotalVerb picture TotalVerb  路  3Comments

omus picture omus  路  3Comments

Keno picture Keno  路  3Comments

arshpreetsingh picture arshpreetsingh  路  3Comments

musm picture musm  路  3Comments