Function values are passed by
reference<http://docs.julialang.org/en/latest/manual/functions/#argument-passing-behavior>,
so you can just add to my.x inside your function:
julia> myadd = function(z,my::mytype)
my.x += z
end
julia> my = mytype()
mytype(1.1)
julia> myadd(3.0,my); my
mytype(4.1)
On Tuesday, April 29, 2014 8:01:56 PM UTC+2, Florian Oswald wrote:
>
> Hi all,
>
> suppose i have a custom type
>
> julia> type mytype
> x :: Float64;
> # constructor
> function mytype()
> new(1.1)
> end
> end
>
> julia> my=mytype()
> mytype(1.1)
>
> and I want to modify x many times, always keeping track of the most recent
> state. suppose x is a large object, so I would like to avoid copies if
> possible. Let's say i want to use function "myadd" on that type:
>
> julia> myadd = function(z,my::mytype)
> return my.x + z
> end
>
> then the question is: is this the fastest way to change x?
>
> julia> my = myadd(3.0,my)
> 4.1
>
> I guess my question could also be: how can I write a myadd!() function?
> obviously this
>
> myadd(3.0,my)
> julia> my.x
> 4.1
>
> leaves x in my unchanged.
>
> thanks
> florian
>