Empty array and AA literals

JR zorael at gmail.com
Wed Apr 9 02:33:46 PDT 2014


On Sunday, 6 April 2014 at 20:14:41 UTC, monarch_dodra wrote:
> 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.

Aye, I'm just saying that there's *incentive* to new it, since it 
allows for reducing the two steps of declaration and setting the 
length into one. (and does compile without warning, at least with 
dmd)


>> Does doing it in two steps allocate twice?
>
> Nope. That's perfectly valid (and recomended).

You'll have to agree though that, by analogy, it's not unlike 
separating declaration from instantiation.

     SomeClass sc;
     sc = new SomeClass();

     string foo;
     foo = "moo";

     int[] arr;
     arr.length = 5;


I would not object to a syntax like T[](length), myself. My main 
pet peeve remains array literals being dynamic.


More information about the Digitalmars-d-learn mailing list