digitalmars.D.learn - how do I get the ith field of a std.typecons.Tuple ?
- Timothee Cour (22/22) Aug 18 2013 A)
- John Colvin (7/12) Aug 18 2013 field is the old name for expand, retained for compatibility,
- Timothee Cour (29/42) Aug 18 2013 I didn't mean Tuple.field (as in Tuple.expand), I really meant the name ...
- Timothee Cour (14/60) Aug 18 2013 and this:
- Dicebot (3/3) Aug 18 2013 Looking at Tuple implementation, this information gets lost at
- John Colvin (3/6) Aug 18 2013 To get the names:
- Joseph Rushton Wakeling (4/10) Aug 18 2013 Just checking, as I'm not too familiar with staticMap, but ... is
- John Colvin (3/16) Aug 18 2013 Doesn't add any any runtime weight at all, it's just an alias.
A) how do I get the ith field of a std.typecons.Tuple ? ideally, it should be as simple as: auto t=Tuple!(int,"name",double,"name2")(1); static assert(t.fields[0] == "name"); It seems the necessary items are private, so how do I get the ith field of a std.typecons.Tuple ? I really don't want to parse T.stringof, which could require a full parser (eg: with Tuple!(A!"bar","name") ) B) Related question: Why isn't slicing and indexing allowed for the Tuple type? eg: alias T=typeof(t); static assert(is(T[0] == int)); static assert(is(T[0..1] == Tuple!(int,"name")); C) Same with appending: static assert(is(T[0..1]~T[1..2] == T)); D) I'm trying to make an append operator but because of problem (A) above I can't (for named Tuples).
Aug 18 2013
On Sunday, 18 August 2013 at 08:46:17 UTC, Timothee Cour wrote:A) how do I get the ith field of a std.typecons.Tuple ? ideally, it should be as simple as: auto t=Tuple!(int,"name",double,"name2")(1); static assert(t.fields[0] == "name");field is the old name for expand, retained for compatibility, it's not recommended. It gives you direct access to the tuple inside the Tuple struct, but it's for getting the variable values, not their names. If you want to get the *names* you've chosen for the tuple fields, you'll have to use traits of some sort I think.
Aug 18 2013
On Sun, Aug 18, 2013 at 2:15 AM, John Colvin <john.loughran.colvin gmail.comwrote:On Sunday, 18 August 2013 at 08:46:17 UTC, Timothee Cour wrote:I didn't mean Tuple.field (as in Tuple.expand), I really meant the name of the corresponding entry, as shown in my example. If you want to get the *names* you've chosen for the tuple fields, you'llA) how do I get the ith field of a std.typecons.Tuple ? ideally, it should be as simple as: auto t=Tuple!(int,"name",double,"**name2")(1); static assert(t.fields[0] == "name");field is the old name for expand, retained for compatibility, it's not recommended. It gives you direct access to the tuple inside the Tuple struct, but it's for getting the variable values, not their names.have to use traits of some sort I think.I don't see how that would work, however I've figured out how to do it: That's a bit of a hack, but should work. Should it be included in phobos, or, better, shall we fix Tuple with some of the recommendations i gave above? ---- import std.typecons; auto tupleField(T,size_t i)()if(isTuple!T && i<T.length){ enum foo0=typeof(T.init.slice!(i,i+1)).stringof; static assert(foo0[$-2..$]==`")`);//otherwise not a tuple with fields enum foo=typeof(T.init.slice!(i,i+1)).stringof[0..$-2]; size_t j=foo.length; while(true){ char fj=foo[--j]; if(fj=='"') return foo[j+1..$]; } } unittest{ import std.typecons; auto t=Tuple!(int,"foo",double,"bar")(2,3.4); alias T=typeof(t); static assert(tupleField!(T,0)=="foo"); static assert(tupleField!(T,1)=="bar"); } ----
Aug 18 2013
and this: auto tupleFields(T)()if(isTuple!T){ string[T.length]ret; foreach(i;Iota!(T.length)) ret[i]=tupleField!(T,i); return ret; } unittest{ import std.typecons; auto t=Tuple!(int,"foo",double,"bar")(2,3.4); alias T=typeof(t); static assert(tupleFields!T==["foo","bar"]); } On Sun, Aug 18, 2013 at 2:26 AM, Timothee Cour <thelastmammoth gmail.com>wrote:On Sun, Aug 18, 2013 at 2:15 AM, John Colvin < john.loughran.colvin gmail.com> wrote:On Sunday, 18 August 2013 at 08:46:17 UTC, Timothee Cour wrote:I didn't mean Tuple.field (as in Tuple.expand), I really meant the name of the corresponding entry, as shown in my example. If you want to get the *names* you've chosen for the tuple fields, you'llA) how do I get the ith field of a std.typecons.Tuple ? ideally, it should be as simple as: auto t=Tuple!(int,"name",double,"**name2")(1); static assert(t.fields[0] == "name");field is the old name for expand, retained for compatibility, it's not recommended. It gives you direct access to the tuple inside the Tuple struct, but it's for getting the variable values, not their names.have to use traits of some sort I think.I don't see how that would work, however I've figured out how to do it: That's a bit of a hack, but should work. Should it be included in phobos, or, better, shall we fix Tuple with some of the recommendations i gave above? ---- import std.typecons; auto tupleField(T,size_t i)()if(isTuple!T && i<T.length){ enum foo0=typeof(T.init.slice!(i,i+1)).stringof; static assert(foo0[$-2..$]==`")`);//otherwise not a tuple with fields enum foo=typeof(T.init.slice!(i,i+1)).stringof[0..$-2]; size_t j=foo.length; while(true){ char fj=foo[--j]; if(fj=='"') return foo[j+1..$]; } } unittest{ import std.typecons; auto t=Tuple!(int,"foo",double,"bar")(2,3.4); alias T=typeof(t); static assert(tupleField!(T,0)=="foo"); static assert(tupleField!(T,1)=="bar"); } ----
Aug 18 2013
Looking at Tuple implementation, this information gets lost at template instatiation time. I think it is worth a pull request to store properly ordered tuple of field aliases in Tuple type.
Aug 18 2013
On Sunday, 18 August 2013 at 18:56:02 UTC, Dicebot wrote:Looking at Tuple implementation, this information gets lost at template instatiation time. I think it is worth a pull request to store properly ordered tuple of field aliases in Tuple type.To get the names: https://github.com/D-Programming-Language/phobos/pull/1493
Aug 18 2013
On Sunday, 18 August 2013 at 19:48:39 UTC, John Colvin wrote:On Sunday, 18 August 2013 at 18:56:02 UTC, Dicebot wrote:Just checking, as I'm not too familiar with staticMap, but ... is that something that will vanish nicely at runtime? That is, it won't add extra weight to the Tuple that must be passed around?Looking at Tuple implementation, this information gets lost at template instatiation time. I think it is worth a pull request to store properly ordered tuple of field aliases in Tuple type.To get the names: https://github.com/D-Programming-Language/phobos/pull/1493
Aug 18 2013
On Sunday, 18 August 2013 at 20:54:14 UTC, Joseph Rushton Wakeling wrote:On Sunday, 18 August 2013 at 19:48:39 UTC, John Colvin wrote:Doesn't add any any runtime weight at all, it's just an alias.On Sunday, 18 August 2013 at 18:56:02 UTC, Dicebot wrote:Just checking, as I'm not too familiar with staticMap, but ... is that something that will vanish nicely at runtime? That is, it won't add extra weight to the Tuple that must be passed around?Looking at Tuple implementation, this information gets lost at template instatiation time. I think it is worth a pull request to store properly ordered tuple of field aliases in Tuple type.To get the names: https://github.com/D-Programming-Language/phobos/pull/1493
Aug 18 2013