Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
March 01, 2008 char[][] titles = (" ", "C", "!", "Description", "Resource", "In Folder", "Location","foo" ); | ||||
---|---|---|---|---|
| ||||
The line below will not compile char[][] titles = (" ", "C", "!", "Description", "Resource", "In Folder", "Location","foo" ); The error is cannot implicitly convert expression ("foo") of type char[3u] to char[][] Is it accepting the first 7 and rejecting the last or does it start from the last in the compiler? How do I fix this? |
March 01, 2008 Re: char[][] titles = (" ", "C", "!", "Description", "Resource", "In Folder", "Location","foo" ); | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ty Tower | Ty Tower wrote:
> The line below will not compile
>
> char[][] titles = (" ", "C", "!", "Description", "Resource", "In Folder", "Location","foo" );
>
> The error is
>
> cannot implicitly convert expression ("foo") of type char[3u] to char[][]
>
> Is it accepting the first 7 and rejecting the last or does it start from the last in the compiler?
> How do I fix this?
>
First it's [..] not (..) for array literals.
Second, current D behavior is to take the type from the first element, rather than doing something useful like figuring out the best type that can hold all the elements.
So you have to tell it you want it to be a list of dynamic arrays by making the first one a dynamic array:
char[][] titles = [" "[], "C", "!", "Description", "Resource", "In Folder", "Location","foo" ];
I think that does it. If not maybe you have to use the [] on all of the strings. I don't recall.
--bb
|
March 01, 2008 Re: char[][] titles = (" ", "C", "!", "Description", "Resource", "In Folder", "Location","foo" ); | ||||
---|---|---|---|---|
| ||||
Posted in reply to Bill Baxter | "Bill Baxter" <dnewsgroup@billbaxter.com> wrote in message news:fqau6f$1565$1@digitalmars.com... > I think that does it. If not maybe you have to use the [] on all of the strings. I don't recall. Just the first. |
March 01, 2008 Re: char[][] titles = (" ", "C", "!", "Description", "Resource", "In Folder", "Location","foo" ); | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | Jarrett Billingsley wrote:
> "Bill Baxter" <dnewsgroup@billbaxter.com> wrote in message news:fqau6f$1565$1@digitalmars.com...
>
>> I think that does it. If not maybe you have to use the [] on all of the strings. I don't recall.
>
> Just the first.
>
>
Using DMD 1.027 it compiles without resorting to that trick on any element.
|
March 01, 2008 Re: char[][] titles = (" ", "C", "!", "Description", "Resource", "In Folder", "Location","foo" ); | ||||
---|---|---|---|---|
| ||||
Posted in reply to John C | John C wrote:
> Jarrett Billingsley wrote:
>> "Bill Baxter" <dnewsgroup@billbaxter.com> wrote in message news:fqau6f$1565$1@digitalmars.com...
>>
>>> I think that does it. If not maybe you have to use the [] on all of the strings. I don't recall.
>>
>> Just the first.
>>
>
> Using DMD 1.027 it compiles without resorting to that trick on any element.
Huh. Coulda sworn that didn't used to work, but it seems like it was working at least as far back as dmd 1.0.
--bb
|
March 01, 2008 Re: char[][] titles = (" ", "C", "!", "Description", "Resource", "In Folder", "Location","foo" ); | ||||
---|---|---|---|---|
| ||||
Posted in reply to Bill Baxter | Bill Baxter wrote: > John C wrote: >> Jarrett Billingsley wrote: >>> "Bill Baxter" <dnewsgroup@billbaxter.com> wrote in message news:fqau6f$1565$1@digitalmars.com... >>> >>>> I think that does it. If not maybe you have to use the [] on all of the strings. I don't recall. >>> >>> Just the first. >>> >> >> Using DMD 1.027 it compiles without resorting to that trick on any element. > > Huh. Coulda sworn that didn't used to work, but it seems like it was working at least as far back as dmd 1.0. There is a difference how this works between array initializers, type inferred array initializers and array literals. The above doesnt work if you change char[][] into auto. Examples: This is an array initializer: (a) char[][] titles = ["a", "bb"]; this is a type inferred array initializer: (b) auto titles = ["a","bb"]; and this is an array literal: (c) auto titles = (["a","bb"]); The rules are slightly different in those cases. In DMD 1.020 (sans a couple of bugs) it works like this: In (a), the type of the initializer is known in advance, and each element of the initializer is implicitly converted to the element type of the array. In (b), the type is first inferred from the initializer (as an array of the the type of the first element) and then each element is implicitly converted like in (a). In (c), the initializer is instead interpreted as an array literal which has a subtle difference from case (b): If the first element of an array literal is a static array, it is converted to a dynamic array. The result is that (a) and (c) compiles, while (b) doesn't(*). *) Depends on compiler version. DMD 2.010 and 2.011 actually compiles but gives incorrect code generation(!): auto titles = ["a","bb"]; writefln(titles); Prints [a b] (!) typeof(titles) is invariant(invariant(char)[1])[2] (c) and (a) prints [a bb] as it should. -- Oskar |
Copyright © 1999-2021 by the D Language Foundation