Tuples citizenship

bearophile bearophileHUGS at lycos.com
Thu Mar 1 15:08:15 PST 2012


Jonathan M Davis:

> I'm sure 
> that there are plenty of cases where using out parameters is actually far 
> cleaner than using tuples, since you don't have multiple return values to deal 
> with.

Are you able to show me one or more examples where using one or more out arguments is in your opinion more clear (and safer!) than using a tuple with the proposed unpacking syntax?

With a tuple you have to deal with multiple return values, but the semantics is cleaner (function => resulting tuple). The number of variables doesn't change, because with "out" you need to define them any way, before the call.

If you think with tuples you lose the names of the out arguments there is a way to avoid this problem (but I don't know if in some situations out arguments are more efficient than tuples):

pure nothrow @trusted real frexp(real value, out int exp);
==>
pure nothrow @trusted Tuple!(real, int,"exp") frexp(real value);


Also, with a tuple result there is no risk of confusion if an argument is "out" (do you remember the discussion where people have asked a ref/out annotation at the calling point too, as in C#? With tuples this problem doesn't exists).

Thank you for your answer,
bye,
bearophile


More information about the Digitalmars-d mailing list