Ok, I have now a working example:

type AutopilotLog
    time::Float64
    counter::Int32
    AutopilotLog() = new();
end

function setfield(x, time::Symbol, value)
        x.time = value
        println("abc")
end

log = AutopilotLog()
println(log)
println(log.time)

setfield(log, :time, 0.1)
println(log)

This gives the output:

julia> setfield_test
AutopilotLog(0.0,0)
0.0
abc
AutopilotLog(0.1,0)
julia>

But I have the impression that there is an error in the documentation. In 
the documentation it says:

setfield(value, name::Symbol, x)
    Assign x to a named field in value of composite type. The syntax a.b = 
c calls setfield(a, :b, c), and the syntax a.(b) = c calls setfield(a, b, 
c).
But I had to implement the function like this:function setfield(x, 
time::Symbol, value)
which means I had to swap x and value. Did I understand something wrong or 
is the documentation wrong?
Regards:
Uwe


On Friday, March 7, 2014 10:40:36 PM UTC+1, andrew cooke 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