Function arguments are evaluated once.

Rewrite 

fill!(instance.ol, Tuple{Int64, Array{Float64}}[])

to

x = Tuple{Int64, Array{Float64}}[]
fill!(instance.ol, x)

and you see that you are filling the instance with the exact same object. 

On Tuesday, October 13, 2015 at 6:52:15 PM UTC+2, [email protected] wrote:
>
> Hi all,
>
> I have this code with two types, one is an observation with a name, a 
> time, and a vector of observed values.
> Another is a system where observations are stored, it comprises a list of 
> names nl which is an index of all the observation names, an observation 
> list ol which is a vector of tuples (t,v) where t is the time of an 
> observation and v is a vector of observed values. I also define a function 
> set to add the observation in the correct place in ol, i.e. at the index of 
> the name of this observation.
>
> Maybe it will be clearer with some code:
>
> type obs
>     n
>     t
>     v
> end
>
> type system
>     nl::Array{AbstractString}
>     ol::Array{Array{Tuple{Int64, Array{Float64}}}}
>     set::Function
>     function system(nl)
>         instance = new()
>         instance.nl = nl
>         instance.ol = Array(Array{Tuple{Int64, Array{Float64}}}, length(
> instance.nl 
> <http://www.google.com/url?q=http%3A%2F%2Finstance.nl&sa=D&sntz=1&usg=AFQjCNHe_eeVBy0vGARxpCZsuW726vgHVQ>
> ))
>         fill!(instance.ol, Tuple{Int64, Array{Float64}}[])
>         instance.set = function(o)
>             push!(instance.ol[findfirst(instance.nl, o.n)], (o.t, o.v))
>         end
>         return instance
>     end
> end
>
> So now, if I try this very simple example:
>
> o1 = obs("b",4,[1.0,3.0])
> o2 = obs("c",6,[2.0,4.0])
> nl = ["a","b","c"]
> s = system(nl)
> s.set(o1)
> println(s.ol[1])
> println(s.ol[2])
> println(s.ol[3])
> println()
> s.set(o2)
> println(s.ol[1])
> println(s.ol[2])
> println(s.ol[3])
> println()
>
> where I defined two observations, a system with a name list, and if I set 
> the two observations in my system, it puts each observation to everywhere 
> in ol, see the output:
>
> Tuple{Int64,Array{Float64,N}}[(4,[1.0,3.0])]
> Tuple{Int64,Array{Float64,N}}[(4,[1.0,3.0])]
> Tuple{Int64,Array{Float64,N}}[(4,[1.0,3.0])]
>
> Tuple{Int64,Array{Float64,N}}[(4,[1.0,3.0]),(6,[2.0,4.0])]
> Tuple{Int64,Array{Float64,N}}[(4,[1.0,3.0]),(6,[2.0,4.0])]
> Tuple{Int64,Array{Float64,N}}[(4,[1.0,3.0]),(6,[2.0,4.0])]
>
> where I was expecting something like:
>
> Tuple{Int64,Array{Float64,N}}[]
> Tuple{Int64,Array{Float64,N}}[(4,[1.0,3.0])]
> Tuple{Int64,Array{Float64,N}}[]
>
> Tuple{Int64,Array{Float64,N}}[]
> Tuple{Int64,Array{Float64,N}}[(4,[1.0,3.0])]
> Tuple{Int64,Array{Float64,N}}[(6,[2.0,4.0])]
>
> Why is an observation pushed everywhere?
>
> I should mention the fact that this code was working perfectly fine under 
> 0.3.11 and I modified it so that it run under 0.4. (I mainly had to change 
> the Tuple syntax.)
>
> Any idea would be welcome, thanks!
>

Reply via email to