Tuple unpacking syntax [Was: Re: Multiple return values...]

bearophile bearophileHUGS at lycos.com
Tue Mar 13 15:26:14 PDT 2012


Andrei Alexandrescu:

> Let me put it another way: I don't see one syntax over another a deal 
> maker or deal breaker. At all.

I am usually able to follow threads, but this time I am a bit lost (this discussion has mixed very different topics like ABIs, implementation efficiency of tuples and typetuples, a hypothetical not-really-a-tuple third-kind of tuple, built-in syntax, library implementation code, etc). Is someone able and willing to summarize the current situation of this discussion?

>From the last weeks (more like months) of intensive usage of functional-style D code I really think D will really enjoy an unpacking syntax for std.typecons.Tuples. Recently I have written a long post about this:

http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=159627

The proposed syntax is quite simple, it's mostly implemented in a patch. The person that has written the patch says he's willing to add the missing part, for foreach looping (see below).

Walter is waiting for something, to find problems. One raw solution for this impasse is to apply the patch as an experiment and then look for problems. Practical usages helps thinking, sometimes.

In this thread I have seen something regarding problems of the patch on code like this, that has one already defined variable:

Tuple!(int,int) foo() { return tuple(1, 2); }
int x;
(x, int y) = foo();


If such code is a problem for the patch, then I am sure the patch author will improve it.

Now I am seeing a scatter()/gather library implementation. In that post of mine there examples like:

alias Tuple!(CTable, string, int, int) Four;
GrowableCircularQueue!Four open;
// ...
while (open.length) {
    immutable item = open.pop();
    immutable CTable cur = item[0];
    immutable string cSol = item[1];
    immutable int x = item[2];
    immutable int y = item[3];


With a tuple unpacking syntax becomes:

while (open.length) {
    immutable (cur, cSol, x, y) = open.pop();


Are those scatter/gather designed to solve this very very simple problem? How do you unpack into immutables? I don't understand.

The missing part in the DMD patch is for something like this (but the exact syntax for this is not yet decided):

foreach (auto (x, y); [tuple(1,2), tuple(3,4)]) {...}

How do you do this with library code?

So I think we should put this thread back on the rails. Library implementations are not enough here. I suggest to start discussing about what's wrong in the proposed D syntax patch, solve the problems Walter has with it.

Bye,
bearophile


More information about the Digitalmars-d mailing list