xxxInPlace or xxxCopy?

spir denis.spir at gmail.com
Fri Jan 21 12:48:57 PST 2011


On 01/21/2011 09:21 PM, Jonathan M Davis wrote:
> The issue is when you don't look at the documentation or trying to avoid having
> to look at the documentation. If you see
>
> auto result = replace(str, "hello", "goodbye");
>
> it's quite clear that a copy is taking place. And if a copy/slice is taking
> place, then that is what you would normally see when replace is used. However,
> if replace alters the array in place, then
>
> replace(str, "hello", "goodbye");
>
> would be what you would normally see. And without looking at the documentation,
> it's not clear whether that is doing it in-place or if you're throwing away the
> return value. However, in the case where replace does a copy/slice, it_is_
> clear, because the return value is saved.

I don't follow you here. You use in your reasoning the particularity of 
C-like funcs which can be both proper functions and action routines. 
Indeed, as you say, one can throw away a result after calling a routine 
which is mainly a function, but for a side-effect; right. But the same 
applies conversely: one can well call a routine which is mainly an 
action (in this case, that operates in-place) and returns whatever 
outcome flag, so that:
	auto result = replace(str, "hello", "goodbye");
actually operates in-place. Which is consistent with its name, an action 
verb suggesting an action. Replace could eg return the number of 
replacements performed (actually useful, what do you think?) Without 
more information, and guessing from the name, that is precisely what I 
would think (and try to imagine what meta-info replace returns).

Do not misinterpret: I actually support the choice of making return/copy 
the default (where both would make sense), because it's safer. But since 
we are changing many names, why not avoid misleading ones, precisely for 
the default case?


Denis
_________________
vita es estrany
spir.wikidot.com



More information about the Digitalmars-d mailing list