Empty array and AA literals

monarch_dodra monarchdodra at gmail.com
Sun Apr 6 13:14:38 PDT 2014


On Sunday, 6 April 2014 at 19:48:37 UTC, JR wrote:
> On Sunday, 6 April 2014 at 09:52:04 UTC, monarch_dodra wrote:
>> An Dynamic Array is merelly a "fat pointer" that holds both 
>> pointer and length. There is no need to create or new a 
>> Dynamic Array.
>
> new allows for setting the length immediately, though.

Right, but what I'm getting at, in regards to the original 
question:
"What's the syntax for a new empty dynamic array"

You don't "new" the dynamic array. Technically, you just allocate 
memory, and then have your *slice* reference that memory. The 
slice itself is not newed.

An empty slice is merely a slice that reference no data. It just 
"exists" and is not "newed".

This is in contrast to, say, classes, where you *must* do "A a = 
new A(args)" to initialize and use it.

And yet even more in contrast to types that "look" like value 
types, but are "secretly" implemented as reference types, without 
the "new" keyword: Types that use reference semantic, but without 
the "new" keyword. These include: AA, Appender, Array, and a 
couple others.

>      auto arr = new int[](99);
>      // arr.length = 99;  // avoided this
>
> Does doing it in two steps allocate twice?

Nope. That's perfectly valid (and recomended).


More information about the Digitalmars-d-learn mailing list