Should slice[]=slice be disallowed?

Ali Çehreli acehreli at yahoo.com
Wed Feb 2 15:42:23 PST 2011


(This must have been discussed before, but I couldn't come up with a way 
to search in the archives.)

There is a hidden danger with two uses of slices:

     double[] slice1 = [ 1, 1, 1];
     double[] slice2 = [ 2, 2, 2];
     double[] slice3 = [ 3, 3, 3];

     slice2 = slice1;              // (1)
     slice3[] = slice1;            // (2)

The last two lines mean very different things:

   (1) start sharing elements with slice1

   (2) copy slice1's elements

If either gets used by accident, the difference may not be noticed until 
the elements are modified in the future.

Could we limit the syntax to reduce the chances of such a risk? e.g. 
"When two slices are involved, both sides must use the array-wise 
syntax". Then the second line had to be written as:

     slice3[] = slice1[];

Ali


More information about the Digitalmars-d mailing list