Regarding fixed size arrays

Chris Williams yoreanon-chrisw at yahoo.co.jp
Fri Mar 14 18:14:26 PDT 2014


On Saturday, 15 March 2014 at 00:11:22 UTC, bearophile wrote:
> Do you think it's useful/worth supporting code like that?

My expectation would be that your code is implicitly the same as:

int[5] foo(int[2] a, int[3] b) {
    int[5] staticArray;
    int[] dynamicArray = a ~ b;
    staticArray = dynamicArray;
    return staticArray;
}

Based on the information at http://dlang.org/arrays.html, my 
expectation would be that a copy requires the slice operator, 
which isn't in your code. Thus it's trying to initialize 
staticArray as a new dynamic array -- which obviously wouldn't 
work.

Since your code doesn't have access to the hidden staticArray 
variable, there's no way to write the correct code:

staticArray[] = dynamicArray;

Short of changing the standard to consider "a = b" to be 
equivalent to "a[] = b" when both a and b are arrays, I don't 
think your code should be handled. And I think such a change 
would be dangerous.

The following works, and I think is probably acceptable.

int[5] foo(int[2] a, int[3] b) {
     int[5] ret;
     ret[] = a ~ b;
     return ret;
}



More information about the Digitalmars-d-learn mailing list