Thread overview
Error: cannot implicitly convert expression ("abcde") of type invariant char[5u] to char[]
Sep 05, 2007
Tom McMurray
Sep 05, 2007
BCS
Sep 05, 2007
Daniel Keep
Sep 05, 2007
torhu
September 05, 2007
I just downloaded version 2.003 or DMD and tried to compile the following :-

class stringtest
{
   void st1()
   {
       char[] s1 = "abcde";
   }
}

This fails with the error :-

Error: cannot implicitly convert expression ("abcde") of type invariant char[5u] to char[]

Is this a bug or am I doing something stupid.  This code compiled successfully on previous versions of the compiler.  Any help would be appreciated
September 05, 2007
Reply to Tom,

> I just downloaded version 2.003 or DMD and tried to compile the
> following :-
> 
> class stringtest
> {
> void st1()
> {
> char[] s1 = "abcde";
> }
> }
> This fails with the error :-
> 
> Error: cannot implicitly convert expression ("abcde") of type
> invariant char[5u] to char[]
> 
> Is this a bug or am I doing something stupid.  This code compiled
> successfully on previous versions of the compiler.  Any help would be
> appreciated
> 

char[] s1 = "abcde"[];


this should work.

(No comment on the issue)


September 05, 2007
Tom McMurray wrote:
> I just downloaded version 2.003 or DMD and tried to compile the following :-
> 
> class stringtest
> {
>    void st1()
>    {
>        char[] s1 = "abcde";
>    }
> }
> 
> This fails with the error :-
> 
> Error: cannot implicitly convert expression ("abcde") of type invariant char[5u] to char[]
> 
> Is this a bug or am I doing something stupid.  This code compiled successfully on previous versions of the compiler.  Any help would be appreciated

D 2.0 is not just the version after D 1.0; it has several breaking changes.  This is not a bug: it is a side effect of the new const system in D 2.0.

If you downloaded it thinking it was simply a newer compiler, you might be well advised to go back to the latest 1.x compiler.  The 1.x series is now stable, and the 2.x series compilers could suddenly change at any moment.

If you downloaded it because you want to play with D 2.0, then I suggest you re-read the language documentation.  There are various bits and pieces that have changed, and it'll save you a lot of grief if you get a handle on it.

	-- Daniel

P.S.  To store a string literal into a char[] in D 2.0, you'll need to use "abcde".dup in order to make a mutable copy of the string.
September 05, 2007
Tom McMurray wrote:
> I just downloaded version 2.003 or DMD and tried to compile the following :-
> 
> class stringtest
> {
>    void st1()
>    {
>        char[] s1 = "abcde";
>    }
> }
> 
> This fails with the error :-
> 
> Error: cannot implicitly convert expression ("abcde") of type invariant char[5u] to char[]
> 
> Is this a bug or am I doing something stupid.  This code compiled successfully on previous versions of the compiler.  Any help would be appreciated

Try 'string' instead of 'char[]'.  String is an alias for 'const(char)[]', which an 'invariant char[5u]' can be implicitly converted to.

And I wouldn't write too much code for D 2.0 just yet, things might change.  There was even talk of changing the const stuff.