Unmatched static array size assignment

bearophile bearophileHUGS at lycos.com
Thu Apr 14 11:50:02 PDT 2011


simendsjo:

> Why is this a bug?

Currently it isn't. But I (and few other people) have asked for it to be a bug, because it sometimes leads to not catching a programmer mistake.


> Static means it's only one instance (per thread), 
> right? The compiler cannot know if you meant to fill the entire array or 
> if you meant something like this
> 
> static string[2] szFormat = ["%s, %s"];
> 
> void main() {
>      szFormat[1] = "%d";
>      assert(szFormat == ["%s, %s", "%d"]);
> }

The idea in the proposal on Bugzilla is that a literal like this is a bug:
static string[2] szFormat = ["%s, %s"];

If you want to fill some space later there is this syntax that explicitly tells the compiler some items are left undefined (Python Zen rule: explicit is better than implicit):
static string[2] szFormat = ["%s, %s", ...];

There is also this syntax to auto determination of the fixed length:
static string[$] szFormat = ["%s, %s"]; // length == 1

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list