[Issue 5603] Initialization syntax for dynamic arrays

d-bugmail at puremagic.com d-bugmail at puremagic.com
Thu Feb 17 04:33:05 PST 2011


http://d.puremagic.com/issues/show_bug.cgi?id=5603


Steven Schveighoffer <schveiguy at yahoo.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |schveiguy at yahoo.com


--- Comment #1 from Steven Schveighoffer <schveiguy at yahoo.com> 2011-02-17 04:30:23 PST ---
This does not need to be a language thing, library could suffice:

auto a = createArray!(int[][])(5, 5, 1); // initialize 5x5 array with the value
1 in each cell.

auto a = createUninitArray!(int[][])(5, 5); // name needs work...

In which we can hide your shown implementation (this can be factored out a
bit).

BTW, your code does not work properly for array appending.  It does not
initialize the hidden "allocated length" field, which would likely result in
reallocation on append.

Some other functions probably needed:

a.extendUninit(size_t newlength);

which is like a.length = newlength but does not initialize the new area.

----------------

I agree a syntax change would be more in line with current array allocation
operations (which are currently all syntax based), but I don't really like your
proposed syntax.

I would propose if we wanted to do a syntax change to do:

auto a = new int[][](5, 5, 1);
auto a = new int[][](5, 5, void);

Where the optional final argument determines the initial value.

This fits perfectly with the current array creation syntax:

new T(dim1, dim2, ..., dimN)

where T is a N dimensional array.  We can just add an extra parameter for the
value.

------------------

One problem with this whole proposal is the issue with struct semantics.  That
is, let's say a struct has a postblit, and you wanted to create an array of
those structs with a default value.  Should the runtime call the postblit for
each element?

I'd say it should.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list