Passing dynamic arrays

Bruno Medeiros brunodomedeiros+spam at com.gmail
Fri Nov 26 11:50:27 PST 2010


On 09/11/2010 12:42, Steven Schveighoffer wrote:
> On Mon, 08 Nov 2010 21:29:42 -0500, Jesse Phillips
> <jessekphillips+D at gmail.com> wrote:
>
>> Well, if you can come up with a good definition for what "increasing
>> the size of a class" would do, then maybe it should be added.
>>
>> It really doesn't matter. Arrays are their own type, the have their
>> own semantics. It does help to think of them in terms of slices (which
>> is what TDPL refers to them as), yet that does not remove the fact
>> that they are in dead a reference type.
>>
>> Many times familiar terms are used so that a behavior is quickly
>> understood. For example it is common to say that arrays in C is just a
>> pointer into a memory location. But in reality that is not true.
>
> It depends on your definition of reference type. I agree with Daniel. If
> you want to get into academic definitions, yes, 'technically' an array
> is a reference, but it's confusing to someone who's not interested in
> exploring the theoretical parts of computer science.
>

What do you mean "depends on your definition of reference type" ? I 
think that what a reference type is, is generally understood fairly well 
by the majority of developers, even if understood implicitly. I think 
our confusion here has more to do to what we consider an "array", rather 
than what we consider to "reference type to be. See below.


> I think of an array as a hybrid between a reference and a value type.
> The data is passed by reference, the length is passed by value. This
> mean changing the length only affects the local copy, but changing the
> data affects all arrays that point to that data.
>

Well, saying _dynamic arrays_ are a hybrid, like you mentioned, is 
perhaps the best way to describe them, with less misunderstanding.

However, if I had to say whether dynamic arrays are value types or 
reference types, I would agree with Jesse and call them reference types, 
and I would not feel this is inaccurate. Let's try a test everyone, look 
at this code:

void test() {
   int[] a = [1, 2, 3];
   int[] b = a;
   int[] c = a;
}

and tell us, what you would you reply if asked "how many arrays are 
created during test's scope"? I would say "1", and not feel it is 
inaccurate. However, if asked how "many dynamic arrays are created 
during test's scope?", I would likely say "3".

This is because what I consider an array to *be*, is its (contiguous & 
homogeneous) elements. With that definition, then D's static arrays are 
value types, because when you assign a static array value to a static 
array variable, the underlying _array_ (ie, the contents), get copied.
Conversely, D's dynamic arrays are reference types, because when you 
assign a dynamic array value to a dynamic array variable, the underlying 
"array" (ie, the contents) are not copied, instead you get two 
references to exactly the same "array" (ie, same data). This viewpoint 
gets a bit murkier because an "array" can be contained inside another 
"array", but that doesn't fundamentally change the it (the viewpoint 
that is).


But ultimately neither view is right or wrong, it just depends on how we 
define our terms, how we conceptualize things. However, it's probably 
best to stick to what the spec/TDPL says about it, if it says anything 
specific. (I don't think it does though :/ ) Or to avoid ambiguous 
designations.


... Oh man, I need to clear my head.
-- 
Bruno Medeiros - Software Engineer


More information about the Digitalmars-d mailing list