Thread overview
Stop function parameters from being copied.
Oct 07, 2010
Benjamin Thaut
Oct 07, 2010
Simen kjaeraas
October 07, 2010
If I want to tell the compiler that a certain function argument should not be copied (say a large struct, or a array) which is the right way to do?

arrays:
1. function foo(in float[] bar) { ... }
2. function foo(ref const(float[]) bar) { ... }
3. something else

structs:
1. function foo(in largestruct bar) { ... }
2. function foo(ref const(largestruct) bar) { ... }
3. something else

Kind Regards
Benjamin Thaut
October 07, 2010
On Thu, 07 Oct 2010 10:43:25 -0400, Benjamin Thaut <code@benjamin-thaut.de> wrote:

> If I want to tell the compiler that a certain function argument should not be
> copied (say a large struct, or a array) which is the right way to do?
>
> arrays:
> 1. function foo(in float[] bar) { ... }
> 2. function foo(ref const(float[]) bar) { ... }
> 3. something else

Arrays are passed by pseudo-reference.  That is, the data is passed by reference, but what part of the data is referred to is passed by value.

so passing an array without any adornments will not copy the array data.

example:

void foo(int[] x)
{
   x = x[3..4]; // does not affect caller's copy
   x[0] = 5; // does affect caller's copy
}

void bar()
{
   int[] x = new int[10];
   foo(x);
   assert(x.length == 10);
   assert(x[3] == 5);
}

> structs:
> 1. function foo(in largestruct bar) { ... }
> 2. function foo(ref const(largestruct) bar) { ... }
> 3. something else

Structs are copied by value, but only a shallow copy.  So if your struct is large, you probably want to use ref.  But if your struct is 'large' because it contains references to large pieces of data, passing by value is ok (similar to arrays).

As a note, in means const scope.  So in largestruct bar is equivalent to scope const largestruct bar.  The scope does nothing, so this can be reduced to const largstruct bar.  This does not pass by reference.

-Steve
October 07, 2010
Benjamin Thaut <code@benjamin-thaut.de> wrote:

> If I want to tell the compiler that a certain function argument should not be
> copied (say a large struct, or a array) which is the right way to do?
>
> arrays:
> 1. function foo(in float[] bar) { ... }
> 2. function foo(ref const(float[]) bar) { ... }
> 3. something else

For arrays, only the pointer and length are passed by value, the data
is passed by reference (said pointer).


> structs:
> 1. function foo(in largestruct bar) { ... }
> 2. function foo(ref const(largestruct) bar) { ... }
> 3. something else

#2.

-- 
Simen
October 07, 2010
On Thu, 07 Oct 2010 14:43:25 +0000, Benjamin Thaut wrote:

> If I want to tell the compiler that a certain function argument should not be copied (say a large struct, or a array) which is the right way to do?
> 
> arrays:
> 1. function foo(in float[] bar) { ... } 2. function foo(ref
> const(float[]) bar) { ... } 3. something else


Just to complement what Steven and Simen have already said, I always find it useful to think of arrays as structs.  For instance, int[] is equivalent to

    struct IntArray
    {
        size_t length;
        int* ptr;
    }

where ptr contains the memory location of the array data.  (In fact, the above is not only a conceptual equivalence.  The struct above is exactly, bit for bit, how a D array is implemented.)

Fixed-size arrays (aka. static arrays), on the other hand, are value types, so there the equivalence goes something like

    // int[3]
    struct IntArray3
    {
        int element0;
        int element1;
        int element2;
    }

Therefore, if you want to pass large fixed-size arrays to a function, you'd better use 'ref' like you would with large structs.

-Lars