Is this a desing rationale? (static array object member)

Bill Baxter dnewsgroup at billbaxter.com
Sat Sep 29 04:53:10 PDT 2007


downs wrote:
> Brian Hsu wrote:
> [snip]
>> So a.z and b.z pointed to same array, but int [] y and int [] z are still different array instance.
>>
>> Regan mentioned that this is because the array literal [1,1,1,1,1] create only one instance of array*, so at first time I suspect that when compiler see [1,1,1,1,1], it would translate that to a fixed memory address of something like that. 
>>
> The reason is that both a.z and b.z were initialized with the _same_
> array literal, whereas y and z were initialized with _different_ array
> literals.
> 
>> Finally, is this behavior reasonable? Since I didn't declare that int [] as a static class member, even though they have same initialization array literal, but I would expect that a.z/b.z they should be different array have same content. (As in Java or C++)
> That's exactly what they are.
> Different arrays with the same content.
> Of course, it helps to know that in D, an array is basically this:
> struct array(T) {
> 	T *ptr;
> 	size_t length;
> }
> So as you see, the memory area that the array uses _is_ the content :)
> 
>> Or is there special reasons of this strange behaver?   
> 
> It's really quite consistent, once you understand what arrays _are_ in D.
>  --downs

Except when you get to the part where arrays are initialized like 
classes instead of structs (x = new ....) and treated like class 
references when compared with null.  In other words the array _syntax_ 
looks mostly like a class, but the _usage_ looks mostly like a struct. 
I don't think its even possible for user created types to act like this. 
  It seems a little odd to me.  But maybe I'm missing something obvious.

--bb



More information about the Digitalmars-d mailing list