Multiple return values...

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Tue Mar 13 07:44:49 PDT 2012


On 3/13/12 4:12 AM, Manu wrote:
> I think I feel a sense of urgency towards the ABI aspect because it is a
> breaking change, and I suspect the longer anything like that is left,
> the less likely/more risky it becomes.
> If it gets delayed for 6-12 months, are you honestly more or less likely
> to say it's a good idea to fiddle with the ABI?

I think Walter could answer that.

> I am sold on the Tuple approach now, so that's a big discussion that can
> be dismissed.

Great!

> Shall we discuss the shortcomings of his implementation? Can someone
> demonstrate the details of his implementation?
>  From the little examples up in the thread, it looked like you could
> only declare new variables inline, but not assign out to existing ones.
> I'd say this needs to be added too, and perhaps that will throw the
> whole design into turmoil? ;)

I thought more about it and we should be fine with two functions (untested):

enum Skip {};
@property ref Skip skip() {
     static __gshared Skip result;
     return result;
}

void scatter(T, U...)(auto ref T source, ref U targets) {
     assert(source.length == targets.length);
     foreach (i, ref target; targets) {
         static if (is(typeof(target) != Skip)) {
             target = source[i];
         }
     }
}

void gather(T, U...)(ref T target, auto ref U sources) {
     assert(target.length == sources.length);
     foreach (i, source; sources) {
         static if (is(typeof(source) != Skip)) {
             target[i] = source;
         }
     }
}

Usage:

auto t = tuple(1, "hi", 2.3);
int a;
string b;
double c;
t.scatter(a, b, skip); // assigns a and b from tuple
b = "!";
++c;
t.gather(skip, b, c); // assigns tuple from variables b and c


Andrei


More information about the Digitalmars-d mailing list