Yup. The reason this works when oVector is a Vector{Float64} is that Julia 
makes a copy in the process of converting it to a Vector{Int} when you 
construct the Cell.

Simon

On Thursday, September 11, 2014 12:53:52 AM UTC-4, John Myles White wrote:
>
> This sure looks like you're not making any copies when you seem to want 
> copies. 
>
> In particular, this line: 
>
> >   cellList[i] = Cell(i, oVector) 
>
> probably needs to be 
>
> >   cellList[i] = Cell(i, copy(oVector)) 
>
>  -- John 
>
> On Sep 10, 2014, at 6:41 PM, Andre Bieler <andre.b...@gmail.com 
> <javascript:>> wrote: 
>
> > can anyone tell me why the following code does not work as (I) expected? 
> > I have a simple type Cell which only has an index and a origin array. 
> > When creating multiple instances of this Cell type in a loop and 
> assigning 
> > different origin arrays to them, in the end all instances have the same 
> > origin array. I am using julia 0.3 
> > 
> > (It does work if the commented line is un-commented though..) 
> > 
> > 
> > type Cell 
> >   index::Int64 
> >   origin::Array{Int64,1} 
> > end 
> > 
> > nDim = int(3) 
> > nCells = int(2) 
> > 
> > cellList = Array(Cell, nCells) 
> > oVector = Array(Int64, nDim)       # does not work as expected 
> > #oVector = Array(Float64, nDim)     # does work 
> > 
> > for i=1:nCells 
> >   for j=1:nDim 
> >     oVector[j] = i 
> >   end 
> >   cellList[i] = Cell(i, oVector) 
> > end 
> > 
> > # after the loop, both cells have same origin... 
> > println(cellList[1].index, ": ", cellList[1].origin) 
> > println(cellList[2].index, ": ", cellList[2].origin) 
> > 
> > 
> > thanks in advance 
> > 
> > andre 
>
>

Reply via email to