The demise of T[new]

dsimcha dsimcha at yahoo.com
Sun Oct 18 20:27:55 PDT 2009


== Quote from Andrei Alexandrescu (SeeWebsiteForEmail at erdani.org)'s article
> dsimcha wrote:
> > == Quote from Andrei Alexandrescu (SeeWebsiteForEmail at erdani.org)'s article
> >> Jason House wrote:
> >>> Andrei Alexandrescu Wrote:
> >>>
> >>>> grauzone wrote:
> >>>>> Walter Bright wrote:
> >>>>>> We both feel that this would simplify D, make it more flexible,
> >>>>>> and remove some awkward corner cases like the inability to say
> >>>>>> a.length++.
> >>>>> How would this remove this corner case?
> >>>> Can't increment length if it's read-only.
> >>>>
> >>>> Andrei
> >>> removing syntactic sugar doesn't really remove corner cases. T[new]
> >>> is being replaced with an array builder. What usage semantics are
> >>> given up with an array builder? How will implicit conversions be
> >>> handled? (alias this, etc...). How will the Phobos array buiilder
> >>> support user specified lengths (for efficiency)?
> >> I think ArrayBuilder could a logic similar to Appender for "~=" and
> >> otherwise offer regular array primitives. I don't think implicit
> >> conversion to T[] is an essential feature, but it can be done.
> >> Andrei
> >
> > So basically, ArrayBuilder would be like T[new], except:
> >
> > 1.  It would be a plain old library type, and the core language would know nothing
> > about it.
> > 2.  T[].dup, T[] ~ T[], new T[3], etc. would return T[], not ArrayBuilder/T[new].
> >
> > Is this basically correct?
> Yes, that's the plan. I hope you're not setting me up or something :o).
> Andrei

Given the issues surrounding T[new] and how ugly some cases of the implicit
conversion can get, you've convinced me that this may be a good solution as long as:

1.  ArrayBuilder implements full array semantics and is usable as a "real" array
as you suggest.  I wasn't aware that this was what was being proposed, as
ArrayBuilder implied to me that it's only good for building arrays as opposed to
being a full-fledged array library type.  Ideally, for convenience it should be
implicitly convertible to T[].
2.  The semantics are such that bug 2093
(http://d.puremagic.com/issues/show_bug.cgi?id=2093) is fixed, while still
allowing appending to arrays of immutables.  This would probably require some
casting under the hood, but the standard lib. is only one step above the core
language, so it's acceptable.  The semantics of the length property would probably
have to be reference semantics, any block of array memory would have to be owned
exclusively by one ArrayBuilder, etc. just like with T[new].



More information about the Digitalmars-d mailing list