Passing dynamic arrays

spir denis.spir at gmail.com
Fri Nov 26 12:56:02 PST 2010


On Fri, 26 Nov 2010 19:50:27 +0000
Bruno Medeiros <brunodomedeiros+spam at com.gmail> wrote:

> 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).

I think your explanation helps & clarify why there are misunderstandings (I mean between people in the D community). Someone used to playing with _static_ arrays in a low-level language is used to identify an array with a memory area. But at a slightly higher level, an array is seen by other people as an element in a program like an int, a struct, a set or whatever; this applies to any kind of array, not only static.
The thin interface implemented by D [the (pointer,length) tuple] creates a minimal abstraction that causes these 2 points of view to diverge in practice. The structure, the array element is not referenced, but the memory area is, well, "pointed".

> 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.

In many languages, there are data structures very similar to D's dynamic arrays, but where the element is referenced as well. So that when newcomers read that D arrays are referenced, they can only think "yes, of course", and fall into the trap.
Note that the "pointing" here has no semantic value, unlike what is usually meant by the notion of reference type; it is instead plain internal mechanics, a necessity to implement a value of variable length.
Either copying the area on assignment (real value type), or on the contrary referencing the element (real ref type), would both result in array types in which these internal mechanics are opaque to the user. Note that I don't advocate for this; I have finally understood the efficiency of D's choice. But it's hard to get.

> ... Oh man, I need to clear my head.

...

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

spir.wikidot.com



More information about the Digitalmars-d mailing list