www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Is there a way to get the list of names of a class' member variables?

reply Jonathan M Davis <jmdavisprog gmail.com> writes:
MemberFunctionsTuple() from std.traits will return the list of names of member 
function, and FieldTypeTuple from std.traits will return the list of the
_types_ 
of the member variables. But I don't see a function that returns the list of
the 
_names_ of the member variables. Am I just blind, or does such a function not 
currently exist?

- Jonathan M Davis
Jul 04 2010
next sibling parent "Simen kjaeraas" <simen.kjaras gmail.com> writes:
Jonathan M Davis <jmdavisprog gmail.com> wrote:

 MemberFunctionsTuple() from std.traits will return the list of names of  
 member
 function, and FieldTypeTuple from std.traits will return the list of the  
 _types_
 of the member variables. But I don't see a function that returns the  
 list of the
 _names_ of the member variables. Am I just blind, or does such a  
 function not
 currently exist?

__traits[1] is your friend in these matters. __traits( allMembers, Foo ) returns a tuple of string literals, each of which corresponds to a member of Foo. [1] http://www.digitalmars.com/d/2.0/traits.html -- Simen
Jul 04 2010
prev sibling next sibling parent "Simen kjaeraas" <simen.kjaras gmail.com> writes:
Simen kjaeraas <simen.kjaras gmail.com> wrote:

 Jonathan M Davis <jmdavisprog gmail.com> wrote:

 MemberFunctionsTuple() from std.traits will return the list of names of  
 member
 function, and FieldTypeTuple from std.traits will return the list of  
 the _types_
 of the member variables. But I don't see a function that returns the  
 list of the
 _names_ of the member variables. Am I just blind, or does such a  
 function not
 currently exist?

__traits[1] is your friend in these matters. __traits( allMembers, Foo ) returns a tuple of string literals, each of which corresponds to a member of Foo.

If you prefer the temmplated way, here's a FieldNameTuple template: template FieldNameTuple( T ) { enum FieldNameTuple = __traits( allMembers, T ); } -- Simen
Jul 04 2010
prev sibling parent Philippe Sigaud <philippe.sigaud gmail.com> writes:
--0016e6d63fb4e9e189048a92d044
Content-Type: text/plain; charset=ISO-8859-1

On Sun, Jul 4, 2010 at 12:55, Simen kjaeraas <simen.kjaras gmail.com> wrote:

 __traits[1] is your friend in these matters.
 __traits( allMembers, Foo ) returns a tuple of string literals, each of
 which corresponds to a member of Foo.


auto ioMembers = __traits(allMembers, std.stdio); It also contains the unittests... Hmm, I wonder what one can do with that. Philippe --0016e6d63fb4e9e189048a92d044 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable <div class=3D"gmail_quote">On Sun, Jul 4, 2010 at 12:55, Simen kjaeraas <sp= an dir=3D"ltr">&lt;<a href=3D"mailto:simen.kjaras gmail.com">simen.kjaras g= mail.com</a>&gt;</span> wrote:<br><blockquote class=3D"gmail_quote" style= =3D"margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); p= adding-left: 1ex;"> <div><div></div><div class=3D"h5"><br><blockquote class=3D"gmail_quote" sty= le=3D"margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204);= padding-left: 1ex;"> __traits[1] is your friend in these matters.<br> <br> __traits( allMembers, Foo ) returns a tuple of string literals, each of<br> which corresponds to a member of Foo.<br> </blockquote><br></div></div></blockquote><div><br>Somehting I discovered t= oday: it works for module names, also:<br><br>auto ioMembers =3D __traits(a= llMembers, std.stdio);<br><br>It also contains the unittests... Hmm, I wond= er what one can do with that.<br> <br><br>Philippe <br></div></div> --0016e6d63fb4e9e189048a92d044--
Jul 04 2010