Setting array length to 0 discards reserved allocation?

Andrew Godfrey via Digitalmars-d digitalmars-d at puremagic.com
Wed Aug 20 20:24:35 PDT 2014


On Wednesday, 20 August 2014 at 00:13:32 UTC, ketmar via 
Digitalmars-d wrote:
> On Tue, 19 Aug 2014 23:58:57 +0000
> Andrew Godfrey via Digitalmars-d <digitalmars-d at puremagic.com> 
> wrote:
>
>> In either case, we are "passing a reference by value".
> yes. but passing null class will not allow to call it's 
> methods, and
> passing null array will. i.e.
>
>   auto foo (MyClass a) { return a.length; }
>
> will crash if length is not a static method (and it can't be 
> static)
> and foo is called like this: foo(null). but the same call will 
> work for
> 'int[]'.
>
> so user can assume that 'int[]' is something similar to class, 
> and it's
> perfectly ok to use int[] methods, and '~=' is just the 
> overloaded
> method, which should change contents of passed dynarray. again,
> overloaded '~=' will work for class (more or less), but will
> inevitably fail (i.e. will not change passed arg) for dynarray.
>
> and it's clear that 'a = new MyClass()' creates new object. and 
> it's
> not clear at all that 'a ~= 42' creates new object too. it's
> counterintuitive.
>
> that's why i think that we need new term.

Well, this is just one case of a general confusion that classes 
and dynamic arrays already share, by virtue of having hidden 
reference semantics (i.e. the "." operator dereferences in some 
cases and not in others, sometimes surprising novices).

You can see this in the existing array doc 
(http://dlang.org/arrays)
where it describes the "sizeof" property for dynamic arrays.
sizeof returns the size of the "dynamic array reference". A 
novice might expect it to return the size of the pointed-to 
array, to act more like the static-array case.


More information about the Digitalmars-d mailing list