Tuples citizenship

bearophile bearophileHUGS at lycos.com
Thu Mar 1 18:06:58 PST 2012


Jonathan M Davis:

> Yes, but chaining functions is the issue. It doesn't work well with tuples 
> unless the function you're passing the result to wants the tuple. If all it 
> wants is one piece of the tuple, then that doesn't work well at all. You're 
> forced to assign the tuple to something else and then call then function 
> rather than chain calls.

In the years I have used a mountain of tuples in Python, but I barely perceive that problem, so I think it's not so bad.


> int exp;
> auto result = frexp(value, exp);
> 
> vs
> 
> auto tup = frexp(value);
> result = tup[0];
> exp = tup[1];

I have assumed to use a sane tuple unpacking syntax. So the second part of your comparison is:

immutable (result, exp) = frexp(value);

That is better than your version with the out argument, safer, looks better, and you are even able to make both results constant.


> Getting tuple return values is annoying. Yes, it can be useful, but most stuff 
> doesn't operate on tuples. It operates on the pieces of tuples. So, you have 
> to constantly break them up. So, using out results in much nicer code.

I think that the tuple unpacking syntax is able to avoid part of your problems. The point of my original post, that maybe was lost in the bulk of the text, was that an unpacking syntax sugar is very useful if you want to use tuples for real in D.

Bye and thank you,
bearophile


More information about the Digitalmars-d mailing list