Thank you. This works like a charm. Also, the name of the function is quite canonical..... (but I did not know it). Have a nice week.
Am Montag, 29. Juni 2015 18:23:42 UTC+2 schrieb Yichao Yu: > > On Mon, Jun 29, 2015 at 12:22 PM, Yichao Yu <[email protected] > <javascript:>> wrote: > > On Mon, Jun 29, 2015 at 12:10 PM, bernhard <[email protected] > <javascript:>> wrote: > >> Hi all > >> > >> assuming I have a composite type > >> > >> type MyType > >> fielda::Int > >> fieldb::Float64 > >> something::UTF8String > >> end > >> > >> x=MyType(2,22.9,"foo") > >> > >> > >> #I can do this > >> x.fielda=-23 > >> > >> function changefield!(x::MyType,fieldname::String,value) > >> > >> x.fieldname=value #this will not work..... > >> > >> return nothing > >> end > >> > >> > > > > ``` > > julia> type T > > a > > b > > end > > > > julia> t = T(1, 2) > > T(1,2) > > > > julia> setfield!(t, :a, "") > > "" > > > > julia> T > > T > > > > julia> t > > T("",2) > > ``` > > > > Or if you'd like a different syntax/flavor > > ``` > julia> t.(:b) = 10 > 10 > > julia> t > T("",10) > ``` > > > > > >> > >> > >> How can I make this function work? It should access the field > "fieldname" > >> and set it to the provided value. > >> I am aware of names(x) . I can also map these symbols to strings. > >> Can eval help me here? I am never quite sure when using eval, as I am > afraid > >> there could be scope issues. > >> > >> Thank you > >> Bernhard >
