Thread overview | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
August 18, 2013 how do I get the ith field of a std.typecons.Tuple ? | ||||
---|---|---|---|---|
| ||||
Attachments:
| 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). |
August 18, 2013 Re: how do I get the ith field of a std.typecons.Tuple ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Timothee Cour | 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.
|
August 18, 2013 Re: how do I get the ith field of a std.typecons.Tuple ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to John Colvin Attachments:
| 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: > >> 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. > 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'll > 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"); } ---- |
August 18, 2013 Re: how do I get the ith field of a std.typecons.Tuple ? | ||||
---|---|---|---|---|
| ||||
Attachments:
| 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: >> >>> 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. >> > > 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'll >> 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"); > } > ---- > > > |
August 18, 2013 Re: how do I get the ith field of a std.typecons.Tuple ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Timothee Cour | 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. |
August 18, 2013 Re: how do I get the ith field of a std.typecons.Tuple ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dicebot | 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 |
August 18, 2013 Re: how do I get the ith field of a std.typecons.Tuple ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to John Colvin | 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:
>> 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
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?
|
August 18, 2013 Re: how do I get the ith field of a std.typecons.Tuple ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Joseph Rushton Wakeling | 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:
>> 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
>
> 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?
Doesn't add any any runtime weight at all, it's just an alias.
|
Copyright © 1999-2021 by the D Language Foundation