xxxInPlace or xxxCopy?

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Wed Jan 19 17:10:07 PST 2011


On 1/19/11 6:53 PM, Jonathan M Davis wrote:
> On Wednesday, January 19, 2011 15:33:16 Andrei Alexandrescu wrote:
>> I'm consolidating some routines from std.string into std.array. They are
>> specialized for operating on arrays, and include the likes of insert,
>> remove, replace.
>>
>> One question is whether operations should be performed in place or on a
>> copy. For example:
>>
>> string s = "Mary has a lil lamb.";
>> // Implicit copy
>> auto s1 = replace(s, "lil", "li'l");
>> assert(s == "Mary has a lil lamb.");
>> // Explicit in-place
>> replaceInPlace(s, "lil", "li'l");
>> assert(s == "Mary has a li'l lamb.");
>
> ++vote;
>
>> So that would make copying the default behavior. Alternatively, we could
>> make in-place the default behavior and ask for the Copy suffix:
>>
>> string s = "Mary has a lil lamb.";
>> // Explicit copy
>> auto s1 = replaceCopy(s, "lil", "li'l");
>> assert(s == "Mary has a lil lamb.");
>> // Implicit in-place
>> replace(s, "lil", "li'l");
>> assert(s == "Mary has a li'l lamb.");
>
> --vote;

So I guess vote stays unchanged :o).

>> Thoughts?
>
> Haven't we been using the approach that string operations generally make copies
> (in many cases slices) and marking functions that do it in place with InPlace?

Problem is, even though the example uses strings, the functions apply to 
all arrays.

Andrei


More information about the Digitalmars-d mailing list