COW vs. in-place.

Dave Dave_member at pathlink.com
Mon Jul 31 13:57:00 PDT 2006


Dawid Ciężarkiewicz wrote:
> Dave wrote:
> 
>> What if selected functions in phobos were modified to take an optional
>> parameter that specified COW or in-place? The default for each would be
>> whatever they do now.
>>
>> For example, toupper and tolower?
>>
>> How many times have we seen something like this:
>>
>> str = toupper(str); // or equivalent in another language.
>>
>> Thanks,
>>
>> - Dave
> 
> I don't get it (the example).
> 
> str = toupper(str);
> 
> does not mean that str can be modified in place
> 
> -- BEGIN --
> void bla(char[] str) {
>   str = toupper(str);
>   /* something else */
> }
> 
> bla("this string is readonly");
> --- END ---
> If you mean something else - sorry, still I don't get it.
> 

Right now, if you call toupper with a string with any lower-case chars. 
in it, it will .dup the string passed into it, then modify the dup'd 
string instead of the original and then return the dup. If it doesn't 
need to modify the string then it returns a reference to the original 
string. Often though, people just want to modify the original string 
anyway, so they do this:

str = toupper(str);

A new version could be declared as:

char[] toupper(char[] s, CIP cip = CIP.COW);

and changed to not modify the original instead of dup'ing it if COW 
isn't specified (which it is by default).

Then instead of str = toupper(str) you could do:

toupper(str,CIP.InPlace);

and avoid the duplication (a ref. to the modified string is still returned).

> I'd rather wait till const/immutability in D problem will be resolved. Don't
> forget that additional "option" is runtime cost. There are some
> propositions of const/immutability that could help providing compile time
> information to deal with your proposition.



More information about the Digitalmars-d mailing list