Changeset 442, implicit Vs explicit

Walter Bright newshound1 at digitalmars.com
Tue Apr 27 07:06:28 PDT 2010


Don wrote:
> bearophile wrote:
>> Do you see a problem here?
>>
>> import std.stdio: writeln;
>> int[24] arr = [10,2,15,15,14,12,3,7,13,5,9,9,7,9,9,9,11,15,1,1,12,5,14];
>> void main() {
>>     writeln(arr);
>> }
>>
>> This code compiles and runs fine, I think according to the D specs, 
>> but it can hide a bug.
> 
>> (Don has split my bug report to avoid fixing the whole problem. I 
>> don't think mine is an enhancement request as Don writes, I think it's 
>> a bug in the D specs.)
> 
> I share your opinion that it's a poor design, but it's not a bug. It's 
> explicitly supported in the spec.

The idea is that for long arrays, often only the start of it needs 
initialization to anything but 0.

> By contrast, a literal with too many items is clearly a bug (it's an 
> array bounds error). Especially when the compiler crashes.

Yes, and thanks for your patch to fix it!

>> A third case is when you really want to define a static array using a 
>> literal that has less items than the static array. I think this is an 
>> uncommon case, what are the real use cases of this? But we might want 
>> to support it anyway. Two examples:
>>
>> int[4] datas = [1, 2];
>> int[3][3] mat = [[1,2,3], [4,5,6]];
>>
>> In D there is another way to write that (using the associative array 
>> syntax! this smells bad):
>> int[4] datas = [0:1, 1:2];
> 
> I do not know why this syntax is supported,

It's so if you only need a few items in a large array initialized. C99 supports 
it as well.

> but it's in the spec. Note 
> that you can even write:
> int [4] datas = [1:2, 3, 0:1];
> That syntax explicitly allows members to remain uninitialized.
> (BTW, it's much, much older than associative array syntax).



More information about the Digitalmars-d mailing list