Check getfield and this also works:
julia> type A
a
b
end
julia> d = {:b => 3, :a=>5}
Dict{Any,Any} with 2 entries:
:b => 3
:a => 5
julia> a = A(-1,-2)
A(-1,-2)
julia> for (k,v) in d
a.(k) = v
end
julia> a
A(5,3)
(You should only use eval in an emergency or when doing meta-programming.)
On Sat, 2014-06-21 at 20:23, [email protected] wrote:
> hi,
> I've got a type and I want to be able to update certain fields of the type
> under the restriction that I don't know when I will be changing with field
> - i.e. I want to be able to do someting like the following. take the
> example type tt with 2 fields, a and b, and a dict "newval" that contains
> new values for both fields. I want to iterate over newval, and put
> newval[j] in tt.j. how can I do this? Here's how far I got.
>
> type tt
> a ::Int
> b ::Int
> end
>
> function update(x::tt,newval::Dict)
> dnames = collect(keys(newval))
> for nm in dnames
> eval(parse("x.$nm = newval[$nm]"))
> end
> return x
> end
> thanks!