On 30/05/13 16:21, Ali Çehreli wrote:
On 05/29/2013 06:54 PM, Peter Williams wrote:
 > Wouldn't a better rule for pass by value be that any changes to
 > the data part of the array (including assignment to an element) causes
 > reallocation of the entire data portion.

The type of a slice parameter is not different than a local slice
variable. Since we wouldn't want an entire copy of the elements due to
an element mutation:

     int[] whole = // ...;
     int[] firstHalf = whole[0 .. $/2];
     firstHalf = 42;    // this should affect whole

Moving the last two lines to a new function should not change meaning:

     int[] whole = // ...;
     setFirstHalf(whole, 42);  // should still affect whole

I think that setFirstHalf() should only effect whole if it's passed in by reference. I certainly intend to adopt that practice in my D code.

Peter

Reply via email to