Make int[12] convertible to int[]

Adam D. Ruppe via Digitalmars-d digitalmars-d at puremagic.com
Wed Dec 17 07:02:31 PST 2014


On Wednesday, 17 December 2014 at 13:13:43 UTC, Shachar Shemesh 
wrote:
> It just seems like extra unneeded superfluous unnecessary 
> redundancy.

It is somewhat important because storing a slice to a static 
array is a big problem:

int[] stored;
void func(int[] s) {
    stored = s;
}

void test() {
   int[12] a;
   func(a);
}

void main() {
    test();
    stored[] = 0; // just overwritten the stack!
}



Making you write the [] doesn't prevent this case, but it does at 
least remind you that a static array and a dynamic array aren't 
completely the same - it gives you a chance to look up the docs 
for func() and figure if it does store, dup it.


Though like bearophile said, the compiler DOES allow this 
implicit conversion at times, which has caused my bugs before. It 
is especially nasty when a char[x] gets implicitly converted to 
string due to this combining with the pure function -> immutable 
thing! There's a function in phobos that will convert like that 
and cause hidden crashes. Nasty.


More information about the Digitalmars-d mailing list