Why string alias is invariant ?

Christian Kamm kamm.incasoftware at shift-at-left-and-remove-this.de
Wed Jan 30 22:10:59 PST 2008


> Overall, the use of invariant 'string' is limited
> to unmodified string literals and their substrings, because for
> other data the behaviour is undefined, by specification.

You need to duplicate your mutable strings:

char[] mutablestring = "hello".dup();
string invariantstring = mutablestring.idup();

Where I think the second line is a shorthand for

string invariantstring = cast(string) mutablestring.dup();

and the cast to invariant is valid since you guarantee to give no one else
access to the newly dupped data.

Christian Kamm




More information about the Digitalmars-d mailing list