Setting array length to 0 discards reserved allocation?

Andrew Godfrey via Digitalmars-d digitalmars-d at puremagic.com
Sun Jul 27 11:52:53 PDT 2014


> The array article does a great job explaining a lot about D 
> arrays, and it definitely has helped people understand them. 
> But it uses the wrong terminology, and that's my point. In D, 
> T[] is a dynamic array and a slice. As far as D is concerned, 
> there is no difference. T[] normally refers to a block of 
> memory that the GC manages, but that block of memory is not 
> typed as a dynamic array. And that's where the article gets it 
> wrong.

Look:

auto c = new MyClass();

The D spec calls c "a reference to a class instance".

This is good terminology because there can be a many-to-one 
relationship between references and classes. In contrast, early 
writing on Java or C# (I forget which) used to call c "an 
object". This was a misguided attempt to hide the fact that 
there's a reference involved - a fact that cannot and should not 
be hidden. And the thing it refers to needs a name more specific 
than "a block of memory".


Now:

int[] i = new int[5];

For now let's call 'i' a "dynamic array" and the thing it refers 
to
a "foobar". The point is still the same: As with object 
references,
there can be a many-to-one relationship between "dynamic arrays" 
and "foobars", and attempts to hide that relationship (e.g. by 
not giving "foobar" a name more specific than "block of memory") 
are misguided.

Furthermore, the concept we're calling a "foobar" here is usually 
called a "dynamic array" in other languages. So IMO D is making 
the same mistake as when people call c above "an object".


More information about the Digitalmars-d mailing list