COW vs. in-place.

Sean Kelly sean at f4.ca
Thu Aug 3 08:14:20 PDT 2006


Oskar Linde wrote:
> Dave wrote:
>> Reiner Pope wrote:
>>>> Why not:
>>>>
>>>>     str = toupper(str);     // in-place
>>>>     str = toupper(str.dup); // COW
> 
> What is the advantage of redundantly assigning the result of an in-place 
> function to itself? In my opinion, all in-place functions should have a 
> void return type to avoid common mistakes such as:
> 
> foreach(e; arr.reverse) { ... }
> // OOPS, arr is now reversed

I like returning the mutated value so the function call can be embedded 
in other code.  And arr.reverse is already a built-in mutating function, 
according to the spec.

> .dup followed by calling an in-place function is certainly ok, but in 
> those cases, an ordinary functional (non-in-place) function would have 
> been more efficient.

Why?


Sean



More information about the Digitalmars-d mailing list