The code
string bar = "Hello ";
const(char) * foo = "world!";
bar ~= foo;
fails to compile in the third line. Why? What elegant alternatives do I have?
The error output is Error: cannot append type const(char)* to type string.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Don’t use a
const(char)*?String literals in D are of type
string, you shouldn’t ever need to use aconst(char)*, except when interfacing with C code.The reason D doesn’t allow that concatenation is because
const(char)*isn’t a string, in any sense of the word. Strings in D areimmutable(char)[](which isalias‘d bystring). Aconst(char)*is just a pointer to a constant character. Unlike C and C++, there is no implied null-terminator, so D can’t and won’t assume there is one.If for whatever reason you absolutely must use a
const(char)*and you know that it is null terminated, then you can make aconst(char)[]out of it by slicing, which you can then append to astring: