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