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



--- Comment #3 from Steven Schveighoffer <schvei...@yahoo.com> 2011-02-17 
05:36:45 PST ---
(In reply to comment #2)
> (In reply to comment #1)
> > 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.
> 
> I see, thank you. That code I have written is clearly bug-prone, that's why a
> built-in syntax (or functions in Phobos) are useful.

I agree, a method to do this correctly would be good to have to avoid people
doing it incorrectly.

> > Some other functions probably needed:
> > 
> > a.extendUninit(size_t newlength);
> > 
> > which is like a.length = newlength but does not initialize the new area.
> 
> This is a possible thing to add. But it looks less useful because when you 
> want
> uninitialized memory, you want max performance, so you probably don't want to
> change the array length.

I'm thinking of the case where I want to add N elements, but I'm going to
assign them one at a time.  This saves the initialization of the N elements
before I write them (a useless operation).

> > 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.
> 
> I am strongly against this idea because it's too much bug-prone. It's too much
> easy to add or remove a [] by mistake, or add or remove the initialization
> value by mistake, so you may end with the wrong number of dimensions, etc.
> auto a = new int[][](5, 5, 2);
> auto a = new int[][][](5, 5, 2);
> auto a = new int[][][](5, 5, 5, 2);

First, this only really happens when the type is numerical.  For example, a
string array would fail to compile with an integral initializer.  Also, a void
initializer cannot be mistaken for a dimension size.

Second, I can see what you are saying, but I don't think this error will affect
much in practice.  It isn't often that one changes the number of dimensions. 
Readability-wise, however, it's not obvious whether the last element is an
initializer (an IDE might make this clearer with syntax coloring).

Your proposal clearly separates the value from the dimensions, but it probably
is unacceptable due to parsing requirements.  Plus it looks very bizarre.

If we are doing syntax changes, I think we need something unorthodox if we want
to make this clear.  What about:

auto a = new int[][](5, 5; 2);
auto a = new int[][](5, 5, =2);
auto a = new int[][](5, 5 : 2);
auto a = new int[][](5, 5) : 2;

I still think the original syntax I proposed is not different from functions
that contain default parameters, it should be able to be dealt with for most
people.  It is also advantageous to try and come up with a reasonable solution
that would be acceptable to the language author.

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

Reply via email to