Changeset 442, implicit Vs explicit

Walter Bright newshound1 at digitalmars.com
Tue Apr 27 07:50:58 PDT 2010


bearophile wrote:
> Walter Bright:
>> The idea is that for long arrays, often only the start of it needs 
>> initialization to anything but 0.
> 
> I got the idea well, but my idea is that it's bug-prone syntax, because it's
> partially implicit (I have put a bug in a program of mine because of that
> syntax). How often do you need that? Is such frequency worth the risk? Can
> you tell me some cases?

I have used it, though I use statically initialized arrays at all very rarely. 
An example is the _ctype[] array which leaves the upper 128 entries unspecified, 
and therefore 0.

> I have not had to use it so far :-) And the syntax
> like the arr[$] = [...] can avoid some of those bugs because you don't need
> to count items.
> 
> And even if you are right, and it's a common need (other people here can
> confirm), then I am asking for an explicit syntax to ask for a partial
> initialization, to avoid bugs.

Anytime you statically initialize an array with more than a small number of values,
it's bug-prone, even if the number of elements match the dimension. There's no check
against transposing entries, or mis-typing the entries themselves.

Your options are to either write unit tests for the table, or have the table 
generated by another program.

Dmd uses program-generated tables for this reason.

>> C99 supports it as well.<
> 
> C is known to be the less safe language among the ones used today (today
> people use C, Java, C++, C#, VB, JavaScript, PHP, Python, Perl, Ruby, Delphi,
> etc).

I know, I just wished to point out that it was not a unique language element.



More information about the Digitalmars-d mailing list