Func Param Confusion - in/out/inout/ref/const/immutable
bearophile
bearophileHUGS at lycos.com
Fri Feb 22 12:30:37 PST 2013
Ali Çehreli:
> > why someone would want to use 'out' instead of 'ref'
>
> There is an idiom in C where a function first sets its "out
> parameter" to null (or another initial value) and then does
> some more work to fill that to actual result of the function:
>
> int make_something(Something ** p)
> {
> int err = 0;
>
> assert(p);
> *p = NULL; // first, initialize
>
> // fill in p later on
>
> return err;
> }
>
> To me, 'out' of D does that initialization automatically.
Another advantage of "out" is that it clearly documents that
argument as another result of the function.
In practice in not-performance critical functions I sometimes
prefer to return multiple values in a tuple, despite D doesn't
yet have a tuple unpack syntax.
One disadvantage of "out" arguments in D is that D (unlike Ada, I
think) doesn't forbid you to set a out argument to some value
before the function call. Such assignment is always useless, and
sometimes it risks being a programmer mistake. A tuple result
doesn't have such bug risk.
Bye,
bearophile
More information about the Digitalmars-d-learn
mailing list