On Mon, Jun 29, 2015 at 12:22 PM, Yichao Yu <[email protected]> wrote:
> On Mon, Jun 29, 2015 at 12:10 PM, bernhard <[email protected]> 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