On Tue, 9 Nov 2010 02:14:17 +0100
Daniel Gibson <metalcae...@gmail.com> wrote:

> On Tue, Nov 9, 2010 at 1:24 AM, Jesse Phillips
> <jessekphillip...@gmail.com> wrote:
> >
> > The array-struct is the reference, so it is what gets compared. That means 
> > both the internal pointer and length must be the same. Just because the 
> > reference is more than an address does not make it any less a reference.
> >
> 
> Unlike in C, a D array is more than a reference.
> An array is the data (or a reference to its data) + metadata (its
> length) - the metadata belongs to the array (and not to the
> reference).
> This means that, when you say "the array is passed by reference" one
> expects that also the arrays length is passed by reference - because
> the length belongs to the array.


Exactly.
When a D object (class instance) holds value or reference fields, changing any 
of them affects other variables denoting the same object, right? This is not 
true for D dyn arrays. D object implement reference semantics, while D arrays 
implement value semantics. Or rather, they do it superficially (shallow copy). 
To have true value semantics, one would need a kind of this(this) copy 
constructor that also copies the target memory area adressed by the array's 
internal pointer.

> > The distinction is that an Array can have its reference changed by resizing 
> > (which is not an option for other reference types).
> >
> 
> If that is not an option for any other reference type, why should it
> be an option for arrays? That is just inconsistent and doesn't make
> any sense.
> 
> No, Arrays should not be considered reference types when passed to a function.
> As someone else said before: Logically you don't pass the array but a
> slice that contains the whole array.

Exactly, again. The comparison with slices makes sense. If I don't mess up 
everything after all those discussions, "b = a;" has the same semantics as "b = 
a[0..$];". A new array struct is built with equal pointer & length as the 
original one. Meaning both arrays _initially_ address the same memory area.
If D arrays were referenced instead, then no copy would happen at all, instead 
a reference to the same struct would be created; so that later changes would be 
shared -- however they internally happen, including reallocation. Like for D 
objects.

For people used to manually implement variable-size data structures (eg in 
plain C), saying that a D dyn array is a kind of (pointer,length) tuple (a fat 
pointer) defining an internal memory area (static array) is also useful. They 
can imagine the internal mechanics and from there infer actual behaviour.

But saying that D dyn arrays are referenced can only bring confusion. And 
stating a parallel with D objects even more: they do _not_ behave the same way.


Denis
-- -- -- -- -- -- --
vit esse estrany ☣

spir.wikidot.com

Reply via email to