Reply to Jarrett,

On Wed, Aug 19, 2009 at 7:01 PM, Justin<[email protected]> wrote:

I was writing some unittests when I ran across some rather unexpected
behavior in which strings in an array were being trimmed to the
length of the first element. Running this program:

import std.stdio;
void main() {
auto strings = ["hello", "cruelly", "innovative", "world"];
writefln(strings);
}
produces this:
[[h,e,l,l,o],[c,r,u,e,l],[i,n,n,o,v],[w,o,r,l,d]]
as the compiler decides to make the strings variable an array of
char[5u]. The problem is easily avoided by replacing auto with
string[], but the problem caught me off guard while working in the
one place where I regularly use auto: unittests.

Is there a reason that the compiler makes the assumptions it does or
is this a bug? I did try searching the bugzilla with a few different
queries, but failed to turn up anything that looked likely.

There's two annoying things going on here:

1) The type of string literals is not char[], it's char[n] where n is
the length of the string.  I don't know why this is.
2) With array literals, the compiler simply determines the type of the
array as being a dynamic array of the type of the first element,
rather than making it an array of the common type of all the elements.
That being said I have no idea why the compiler is allowing "cruelly",
which is of type char[7], to be implicitly converted to char[5].  I
thought that was not legit.


the solution is to use

auto strings = ["hello"[], "cruelly", "innovative", "world"];


Reply via email to