Why I (Still) Won't Use D
Janice Caron
caron800 at googlemail.com
Fri Mar 28 02:53:44 PDT 2008
On 28/03/2008, Walter Bright <newshound1 at digitalmars.com> wrote:
> Making strings arrays of invariant makes them implicitly copy-on-write.
We're drifting off topic here, but ... I don't think that's right.
Example:
string s = "hello";
string t = std.string.replace(s,"x","y");
assert(s == t); // OK
assert(s is t); // *FAILS*
This example demonstrates that std.string.replace() does not do
copy-on-write. (Instead, it does copy-always). Thus, as I was saying,
whether or not copy-on-write is done is part of the algorithm, not
part of the type. Of course, std.string.replace() could be rewritten
to implement copy-on-write, but that would kinda prove my point.
Invariant arrays make copy-on-write /possible/, but they don't make it
mandatory.
> More generally, whether D arrays are copy-on-write or not is based on
> whether the element type is invariant or mutable.
Again, it is /possible/ to implement copy-on-write using mutable
strings (as MSVC++ std::string proves) - just very difficult. But D's
immutable makes it easy.
> It isn't based on if
> the contents are text or not.
No argument there! :-)
More information about the Digitalmars-d
mailing list