Array literals
Nick Sabalausky
a at a.a
Fri Oct 17 13:00:55 PDT 2008
"bearophile" <bearophileHUGS at lycos.com> wrote in message
news:gd9vqi$oos$1 at digitalmars.com...
> Nick Sabalausky:
>> I like T[static] a lot. Static might be a highly overloaded word, but the
>> idea of a "static array" is already one of the firmly established
>> overloads.
>> You want a static array? Say "static". Nice :)
>
> +1.
> I think it's acceptable. So the code equivalent to the original one
> becomes (plus the immutable statement):
>
> import std.stdio: writefln;
> void main() {
> char[static][static] a = ["Hello", "what"];
> writefln(a[2].length); // 5
> }
>
> Most of the times you don't want that so you use:
>
> auto a = ["Hello", "what"];
>
> Now we can wait for Walter to express his opinion on this improvement of
> the language.
>
> Bye,
> bearophile
As long as we're talking about initializing jagged static arrays, I'd also
add one other slight change. The following should also compile (the current
D1 equivilent code complains. Not sure about D2):
// The only change from the code above is
// swapping the order of "Hello" and "what"
import std.stdio: writefln;
void main() {
char[static][static] a = ["what", "Hello"];
writefln(a[1].length); // 5
}
Might just be a bug, but currently (assuming the T[static] got implemented),
the above would fail to compile with an error complaining that it cannot
convert "Hello" from type char[5u] to type chat[4u]. Apperently,
"a[anything].length" is assumed to be "a[0].length" instead of
"max(a[0].length, a[1].length,...a[$-1].length)". So, currently, a[0].length
must be >= the longest of the rest of the strings. That's bitten me a few
times already.
More information about the Digitalmars-d
mailing list