COW vs. in-place.

Tom S h3r3tic at remove.mat.uni.torun.pl
Thu Aug 3 16:01:54 PDT 2006


Oskar Linde wrote:
> Kirk McDonald wrote:
>> renox 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.
>>>
>>>
>>> In ruby, they have this nice convention that a.function() leaves a 
>>> unchanged and a.function!() modifies a.
>>>
>>> Something like this would be nice, the hard part is choosing the 
>>> correct naming convention so that it is followed..
>>>
>>> functionXIP (eXecute In Place), functionWSD (With Side Effect)?
>>> Sigh, hard to achieve something as simple and elegant as '!' : 
>>> caution this function modifies the object!
>>>
>>> In the absence of proper naming termination, an optionnal parameter 
>>> could be used yes.
>>>
>>
>> What about:
>>
>> void   toupper(char[] s);  // Modifies s in-place
>> char[] asupper(char[] s);  // COW function
>>
>> Of course, this convention would only apply to functions named 
>> "tosomething", but I bet most/all of the functions for which an 
>> "in-place" operation makes sense are named that.
> 
> It doesn't really apply to functions that are verbs, like capitalize, 
> sort and map.
> 
> For those one option is: capitalized, sorted and mapped for COW versions.

I know we aren't supposed to like pointers, but it could also work the 
following way:

void   toupper(char[]* s);  // modifies *s in-place
char[] toupper(char[] s);   // moo

then by writing:

toupper(&foo);

you'd make it pretty clear that foo is to be modified. Internally, the 
in-place version could immediately call sth like
void toupper_inPlace(inout char[] s);


--
Tomasz Stachowiak



More information about the Digitalmars-d mailing list