Tuples citizenship

Jonathan M Davis jmdavisProg at gmx.com
Fri Mar 2 10:15:57 PST 2012


On Friday, March 02, 2012 12:59:43 kennytm wrote:
> Jonathan M Davis <jmdavisProg at gmx.com> wrote:
> > On Friday, March 02, 2012 09:53:19 kennytm wrote:
> >> You can just chain with
> >> 
> >> return doSomething(findSplit(haystack, needle)[0]);
> >> 
> >> if you just need the return value. Compare with 'out':
> >> R4 ignored;
> >> R5 ignored2;
> >> return doSomething(findSplit(haystack, needle, ignored, ignored2));
> >> 
> >> How do you chain with _partial_ amount of return values with 'out'?
> > 
> > If the function uses out, then you can chain the return value without
> > losing the values which were assigned to the out arguments, but if you
> > have a tuple, and you select one of the elements in the tuple to chain,
> > you lose the others. The only way to get _all_ of the values in the tuple
> > is to assign the tuple to a variable, in which case, you can't chain at
> > all.
> > 
> > - Jonathan M Davis
> 
> I see what you mean. However, this is useful only when you know one of the
> return value is special, and make the rest 'out' parameters, e.g.
> 
> File openFile(string fn, string mode, out ErrorCode errCode);
> 
> because the API designer know people seldom focus on the 'errCode'. But if
> not all return values are considered unimportant (such as findSplit and
> remquo), randomly making some of them as the 'out' parameter just make the
> normal use cases more messy.
> 
> Besides, you can't use type inference with 'out' parameters. You don't
> actually know the types R4 and R5 in my findSplit2 example.

It's not like using out is fantastic and tuple sucks. They both have pros and 
cons. My point is that it's not the case that always returning tuples is 
better, which seems to be the point that Bearophile is trying to push. There 
_are_ downsides to returning tuples. Whether a tuple or an out parameter is 
better depends on the function and the context in which it is used.

- Jonathan M Davis


More information about the Digitalmars-d mailing list