[Sharing]Tuple to variables (My Function)

MattCoder mattcoder at hotmail.com
Fri Jun 14 16:56:58 PDT 2013


Hi,

Sooner I asked about this ([Doubt] Variadic arguments as 
reference (Possible?) - - 
http://forum.dlang.org/thread/jwujjhjizkovvmbegaio@forum.dlang.org).

In fact my intend was to recreate a feature that I like in 
Python, where you can assign variables from a tuple, e.g.:

pos = (10, 20)
x,y = Pos

or

RGB = (255,255,255)
r,g,b = RGB

Thanks to the fellows who help me early morning, I wrote the 
workable version below:

import std.stdio;
import std.typecons;
import std.variant;

void tupleToVar(A, T...)(A inTuple, ref T listParams){
   foreach(i, ref p; listParams){
     if(!(i<inTuple.length))
       break;

     p = inTuple[i];
   }
}

void main()
{
     auto myTuple = tuple(1,2,3,"Test");
     Variant a, b, c, d;
     tupleToVar(myTuple, a, b, c, d);
     writeln(a);
     writeln(b);
     writeln(c);
     writeln(d);
}

dpaste: http://dpaste.dzfl.pl/6f3f25d0

It would be nice if it could appear more like python, but I think 
this would be a internal dev.

Matheus.


More information about the Digitalmars-d-learn mailing list