Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
May 31, 2012 Merge tuples | ||||
---|---|---|---|---|
| ||||
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 Re: Merge tuples | ||||
---|---|---|---|---|
| ||||
Posted in reply to simendsjo | 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 Re: Merge tuples | ||||
---|---|---|---|---|
| ||||
Posted in reply to Alex Rønne Petersen | On Thu, 31 May 2012 16:54:53 +0200, Alex Rønne 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 values. >> 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 Re: Merge tuples | ||||
---|---|---|---|---|
| ||||
Posted in reply to simendsjo | 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 Re: Merge tuples | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | 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 Re: Merge tuples | ||||
---|---|---|---|---|
| ||||
Posted in reply to simendsjo | 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...) { >>> ? >>> } Is the following what you're looking for? import std.stdio; import std.typecons; import std.typetuple; template TypeAndName(Variables...) { static if (Variables.length == 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 = 1; double d = 3.14; auto t = toTuple!(i,d); writeln(t.i); writeln(t.d); } |
Copyright © 1999-2021 by the D Language Foundation