Since they are immutable, fill! did exactly what you wanted

On Friday, May 16, 2014, Tim Holy <tim.h...@gmail.com> wrote:

> Try
>
> arr = [ChannVals() for i = 1:10]
>
> On Friday, May 16, 2014 01:27:18 AM Carlos Becker wrote:
> > Hello all,
> >
> > I wanted to create an array of an immutable type and initialize an empty
> > copy in each (with the default constructor).
> > I am wondering which is the best way to do it, so far:
> >
> >    immutable ChannVals
> > taus::Vector{Float64}
> > alphas::Vector{Float64}
> >
> > ChannVals() = new( Float64[], Float64[] )
> >    end
> >
> >    # create 10 new instances
> >    arr = ChannVals[ChannVals() for i=1:10]
> >
> >
> > Now, a neat but incorrect way is to do
> >
> >    arr = Array( ChannVals, 10 )
> >    fill!(allVals, ChannVals())
> >
> > because it will fill them with the same instance.
> > Is there a neat way, such as a fillwithcopies!() ?
> >
> >
> > Cheers.
>

Reply via email to