automatic conversion to invariant (string?)
Janice Caron
caron800 at googlemail.com
Thu Mar 20 08:32:09 PDT 2008
On 20/03/2008, Yossarian <xtauer01 at stud.fit.vutbr.cz> wrote:
> > char[] s = cast(char[]) "hello";
>
> i think that
> char[] s = "hello" ; // this should compile without explicit cast. imho.
In fact, I just wrote that for illustration purposes. I was trying to
imply that s was a char[], pre-initialised with "hello". In real code,
you should /never/ do
char[] s = cast(char[]) "hello";
so I was guilty of using a really bad example. The reason you should
never use it is because casting away invariance is undefined, and in
this case, the literal might be in a ROM segment. The correct way to
do it should be, as others have pointed out:
char[]s = "hello".dup;
> // int[] c = [3, 5]; compiles.
In the future, it probably won't. Don't rely on it.
> string t = s; // wouldn't it be logical to use not copy-on-write, but
> initialize new string with old char[]?
Since that line won't compile anyway, the question is moot.
> s[0] = 'j'; // change only s, not t.
No can do. A string is nothing but a struct consisting of { ptr,
length }. Element assignment should not cause a heap allocation.
More information about the Digitalmars-d
mailing list