Returning tuples from functions

BCS BCS at pathlink.com
Tue May 1 11:02:44 PDT 2007


niovol wrote:
> When will it be possible? I have almost no experience in programming on D. But I can't imagine how to solve my problem without using tuples.

Here's a nasty hack I came up with a while ago

struct SetT(V...)
{
   V args_m;
   void opCall(inout V args)
   {
     foreach (i, arg; args_m) args[i] = arg;
   }
}

SetT!(V) Set(V...)(V args)
{
   SetT!(V) ret;
   foreach (i, arg; args) ret.args_m[i] = arg;
   return ret;
}

//// Usage


SetT!(int,int) bar(int i, int j){ return Set(i,j); }

void main()
{
   int i=1,j=2,k=0,l=0;
   bar(i,j)(k,l);
   writef("[k,l]=[%d,%d]\n", k,l);
}


note: assignment is left-to-right



More information about the Digitalmars-d mailing list