On Tue, 21 Jun 2011 23:02:43 +0000, Jonathan M Davis wrote: > On 2011-06-21 15:25, Ali Çehreli wrote: >> (Note: I have a feeling that this must be related to the old 'unique' >> discussions, which I had somehow managed to stay out of. If so, I >> apologize for repeating an old story.) >> >> It is most useful for a function to return the most mutable type unless >> there is good reason not to. Do you agree? > > No. In a lot of cases, what you generally want is immutable, not > mutable. In particular, if you're talking about strings, we favor > string, wstring, and dstring over char[], wchar[], or dchar[] unless you > actually need to alter the string.
I agree with all of that, but it should not be the function that restricts the caller. The function should return immutable only when the result is really immutable. If the returned object is mutable and unique, it is pretentious of the function to evangelically return immutable. :) Besides, the caller can't know whether it was actually mutable but was returned as immutable because it was the right thing to do. Otherwise the caller could cast to mutable to save a copy. > immutable is preferred, because it's generally more efficient. Not in this case because the caller must make a copy to mutate further. If the function could return 'unique mutable' and if that could be casted to immutable implicitly, then all would be fine. Ali
