There's also fill(instance, dims).

But be aware of one subtlety: you'll be creating an array of references to the 
same object. Example:

julia> a = [1]
1-element Array{Int64,1}:
 1

julia> b = fill(a, 2)
2-element Array{Array{Int64,1},1}:
 [1]
 [1]

julia> b[1][1] = 2
2

julia> a
1-element Array{Int64,1}:
 2

julia> b
2-element Array{Array{Int64,1},1}:
 [2]
 [2]


In contrast:
julia> b = [[1] for i = 1:2]
2-element Array{Array{Int64,1},1}:
 [1]
 [1]

julia> b[1][1] = 2
2

julia> b
2-element Array{Array{Int64,1},1}:
 [2]
 [1]


This way you're constructing a brand-new array for each element of b.

--Tim

On Friday, July 25, 2014 11:43:15 PM Ross Boylan wrote:
> On Fri, 2014-07-25 at 17:36 -0700, Ross Boylan wrote:
> > I think I've narrowed down my previous problem.  I have a composite type
> > that has members, some of which are arrays.  How do I initialize those
> > inner arrays properly?  Actually, I have an earlier problem: the objects
> > aren't being created at all.  Here's what I tried (nstep and npop
> > previously set):
> > julia> type B; i::Vector{Int}; end
> > 
> > julia> B() = B(zeros(Int, nstep))
> > B (constructor with 3 methods)
> > 
> > julia> typeof(B)
> > DataType
> > # but isn't B also the name of a function?
> > 
> > julia> bs = Array(B, npop)
> > 
> > 4-element Array{B,1}:
> >  #undef
> >  #undef
> >  #undef
> >  #undef
> > 
> > My hope was that defining what would be a default constructor in C++,
> > name B(), would establish the initial values for the vector elements
> > bs[i].  It didn't.
> > 
> > How do I create an Array of B's that are properly initialized?
> 
> Thanks to John and Kevin for their solutions.  I also notice the fine
> manual says that Array provides "an uninitialized dense array".  Just
> below that is another solution (after creating the array):
> fill!(A, x)   fill the array A with value x
> 
> For reference, the other 2 solutions were
> bs = Array(B, npop)
> for i in 1:npop
>   bs[i] = B()
> end
> 
> or
> 
> [B() for i=1:npop]
> 
> I looked for, but did not find, a method that would create an array out
> of a protypical instance.
> 
> Ross

Reply via email to