String Literals (D 2.0)
Vladimir Panteleev
thecybershadow at gmail.com
Mon Jun 18 22:49:52 PDT 2007
On Tue, 19 Jun 2007 00:36:10 +0300, Xinok <xnknet at gmail.com> wrote:
> I find this a bit annoying:
> char[] str = "Test String";
> Error: cannot implicitly convert expression ("Test String") of type
> invariant char[11] to char[]
String literals will be placed in "read-only" memory while compiling. Writing to this area should generate an exception, although it doesn't on Windows. If you'd like to change the string's contents after initializing it, consider writing
char[] str = "Test String".dup;
> Yet if you write:
> int[] str = [10, 20, 30];
> It gives no error.
I'm not sure whether integer literals are also placed in the same data segment for read-only memory. If it is, then it's a bug - all array literals should be immutable.
> I think the default behavior for string literals should be the same as
> array literals. And if the user does want a const or invariant string:
> const str = "Test String";
> invariant str = "Test String";
My opinion is just that there should be a simple (castless, .dup-less) way to put string and array literals into the initialized data segment - that is, where you give it an initial value but can still write to it.
--
Best regards,
Vladimir mailto:thecybershadow at gmail.com
More information about the Digitalmars-d
mailing list