tuples are your friends

Timon Gehr timon.gehr at gmx.ch
Sat Oct 1 09:33:08 PDT 2011


On 10/01/2011 12:17 PM, Gor Gyolchanyan wrote:
> I wonder why aren't tuples fully supported in D?
>
> string, int, float createTuple()
> {
>      return "hello", 5, 6.7f;
> }
>
> unittest
> {
>      string s;
>      int i;
>      float f;
>
>      s, i, f = createTuple();
> }
>
> Wouldn't this be a cleaner way of dealing with static-length
> heterogeneous collections of variables?
> The comma operator would have a single definite meaning: creating a
> tuple out of left-hand variable or tuple and right-hand variable.
> The existing use of comma wouldn't change:
> * All functions will technically take 1 argument only: a variable or a tuple.
> * Comma-separated expressions are technically a single tuple expression.
> * UFCS will get expanded to include tuples: ("/", "usr",
> "bin").buildNormalizedPath;
>
> It's easy to imagine the same thing but with support for tuples
> consisting of any symbols, and nut just variables.

This interferes with the comma operator. A, B; will evaluate A and then 
B and the result of the expression is the result of B. I agree that 
built-in tuples would be really nice. Unfortunately, making them nice 
would amount to a breaking change. There is a nice pull request by Kenji 
Hara that would allow this:

auto (a,b,c) = createTuple();

For an appropriate definition of createTuple (function returning library 
tuple/range/...)







More information about the Digitalmars-d mailing list