mutable, const, immutable guidelines
H. S. Teoh
hsteoh at quickfur.ath.cx
Thu Oct 17 20:27:39 PDT 2013
On Thu, Oct 17, 2013 at 08:56:08AM +0200, qznc wrote:
> On Wednesday, 16 October 2013 at 20:33:23 UTC, H. S. Teoh wrote:
> >I'm of the view that code should only require the minimum of
> >assumptions it needs to actually work. If your code can work with
> >mutable types, then let it take a mutable (unqualified) type. If your
> >code works without modifying input data, then let it take const. Only
> >if your code absolutely will not work correctly unless the data is
> >guaranteed to never change, ever, by anyone, should it take
> >immutable.
>
> Sounds reasonable. Which one of the following would you prefer?
>
> foo1(const T t) {
> takes_immutable(t.idup);
> }
>
> foo2(immutable(T) t) {
> takes_immutable(t);
> }
>
> I'd say foo2, which puts the burden of the copy on the caller.
> However, if the caller already has an immutable value an unecessary
> copy is avoided.
Well, that's a bit too generic to really decide one way or the other.
What do foo1 and foo2 do? I don't think it's fair to say one or the
other is superior without more context. It depends on what you're trying
to achieve.
T
--
Too many people have open minds but closed eyes.
More information about the Digitalmars-d-learn
mailing list