Implicit conversion of unique objects to mutable and immutable
Jonathan M Davis
jmdavisProg at gmx.com
Tue Jun 21 17:31:19 PDT 2011
On 2011-06-21 16:50, Ali Çehreli wrote:
> 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.
As I said, in Phobos, when a new string must be allocated, the type returned
is generally string (so it's immutable), whereas if the result of the function
is likely to be slice of the string passed in, then the function is templated
to return the same type of string as was passed to it. There's no extraneous
copying going on in order to return string. The _only_ case where it's
arguably forced on the caller is in the case where a copy _must_ be made, in
which case the function returns string rather than char[].
- Jonathan M Davis
More information about the Digitalmars-d-learn
mailing list