On master Julia (not 0.2.x) you can also use a field index with getfield
and setfield!, which is sometimes handy.


On Fri, Mar 7, 2014 at 4:40 PM, andrew cooke <[email protected]> wrote:

>
> julia> type Foo
>          a::Int
>        end
>
> julia> foo = Foo(3)
> Foo(3)
>
> julia> setfield(foo, :a, 5)
> WARNING: setfield is deprecated, use setfield! instead.
>  in setfield at deprecated.jl:8
> 5
>
> julia> setfield!(foo, :a, 5)
> 5
>
> julia> foo
> Foo(5)
>
>
> On Friday, 7 March 2014 18:35:25 UTC-3, Uwe Fechner wrote:
>>
>> Ok, even if overloading the dot operator is not yet implemented I would
>> be pleased if someone could provide an example how to use the setfield
>> function.
>>
>> Uwe
>>
>> On Friday, March 7, 2014 10:20:51 PM UTC+1, Ivar Nesje wrote:
>>>
>>> I do not think you can overload the . operator in Julia yet.
>>> Setfield is only for calling to set the value.
>>>
>>> See also https://github.com/JuliaLang/julia/issues/1974
>>>
>>> kl. 22:16:09 UTC+1 fredag 7. mars 2014 skrev Uwe Fechner følgende:
>>>>
>>>> Hello,
>>>>
>>>> I am looking for an example how to use the setfield function.
>>>> (see: http://docs.julialang.org/en/release-0.2/stdlib/base/ )
>>>>
>>>> The following does not work (setfield is not called when the field time
>>>> is set):
>>>>
>>>> using ProtoBuf
>>>> import ProtoBuf.meta
>>>>
>>>> type AutopilotLog
>>>>     time::Float64
>>>>     counter::Int32
>>>>     number::Int32
>>>>     AutopilotLog() = (x = new(); fillunset(x); x)
>>>> end #type AutopilotLog
>>>> meta(t::Type{AutopilotLog}) = meta(t, Symbol[:time,:counter], Int[],
>>>> Dict{Symbol,Any}())
>>>> function setfield(value, time::Symbol, x)
>>>>         x.time = value
>>>>         println("abc")
>>>> end
>>>>
>>>> log = AutopilotLog()
>>>> log.time = 0.0
>>>>
>>>> Any hint what I am doing wrong?
>>>>
>>>> Regards:
>>>>
>>>> Uwe (using Julia 0.21)
>>>>
>>>

Reply via email to