www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Merge tuples

reply simendsjo <simendsjo gmail.com> writes:
This is probably very simple, but I cannot find out how to do it..

I have several variables, and want to construct a tuple for the values.
int a;
string b;
ToTuple!(a, b) should create Tuple!(int, "a", string, "b")

template ToTuple(Variables...) {
   ?
}
May 31 2012
next sibling parent reply =?UTF-8?B?QWxleCBSw7hubmUgUGV0ZXJzZW4=?= <alex lycus.org> writes:
On 31-05-2012 16:53, simendsjo wrote:
 This is probably very simple, but I cannot find out how to do it..

 I have several variables, and want to construct a tuple for the values.
 int a;
 string b;
 ToTuple!(a, b) should create Tuple!(int, "a", string, "b")

 template ToTuple(Variables...) {
 ?
 }
Just look at std.typecons.Tuple. -- Alex Rønne Petersen alex lycus.org http://lycus.org
May 31 2012
parent simendsjo <simendsjo gmail.com> writes:
On Thu, 31 May 2012 16:54:53 +0200, Alex R=C3=B8nne Petersen <alex lycus=
.org>  =

wrote:

 On 31-05-2012 16:53, simendsjo wrote:
 This is probably very simple, but I cannot find out how to do it..

 I have several variables, and want to construct a tuple for the value=
s.
 int a;
 string b;
 ToTuple!(a, b) should create Tuple!(int, "a", string, "b")

 template ToTuple(Variables...) {
 ?
 }
Just look at std.typecons.Tuple.
I did, but I don't know how to lower it so I don't get tuples of tuples:= = Tuple!(int,"a"c,Tuple!(string,"b"c))
May 31 2012
prev sibling parent reply "bearophile" <bearophileHUGS lycos.com> writes:
simendsjo:

 int a;
 string b;
 ToTuple!(a, b) should create Tuple!(int, "a", string, "b")

 template ToTuple(Variables...) {
   ?
 }
ToTuple will have a hard time inventing the names for those fields... Bye, bearophile
May 31 2012
parent reply simendsjo <simendsjo gmail.com> writes:
On Thu, 31 May 2012 18:42:20 +0200, bearophile <bearophileHUGS lycos.com>  
wrote:

 simendsjo:

 int a;
 string b;
 ToTuple!(a, b) should create Tuple!(int, "a", string, "b")

 template ToTuple(Variables...) {
   ?
 }
ToTuple will have a hard time inventing the names for those fields... Bye, bearophile
Not sure I understand what you mean. Using Variables[i].stringof in a template gives the original name.
May 31 2012
parent Philippe Sigaud <philippe.sigaud gmail.com> writes:
On Thu, May 31, 2012 at 7:14 PM, simendsjo <simendsjo gmail.com> wrote:
 On Thu, 31 May 2012 18:42:20 +0200, bearophile <bearophileHUGS lycos.com>
 wrote:

 simendsjo:

 int a;
 string b;
 ToTuple!(a, b) should create Tuple!(int, "a", string, "b")

 template ToTuple(Variables...) {
 =C2=A0?
 }
Is the following what you're looking for? import std.stdio; import std.typecons; import std.typetuple; template TypeAndName(Variables...) { static if (Variables.length =3D=3D 0) alias TypeTuple!() TypeAndName; else alias TypeTuple!(typeof(Variables[0]), Variables[0].stringof, TypeAndName!(Variables[1..$])) TypeAndName; } Tuple!(TypeAndName!(Variables)) toTuple(Variables...)() property { return Tuple!(TypeAndName!(Variables))(Variables); } void main() { int i =3D 1; double d =3D 3.14; auto t =3D toTuple!(i,d); writeln(t.i); writeln(t.d); }
May 31 2012