www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - get only user-defined members

reply Marc <jckj33 gmail.com> writes:
how do I from class:

 class Person {
  string name;
  int age;
 }
do:
 auto c = [__traits(allMembers, Person)];
then return only ["name", "age"] rather ["name, "age", "ctor", "toString" ... ]?
Dec 15 2017
parent reply Jonathan M Davis <newsgroup.d jmdavisprog.com> writes:
On Saturday, December 16, 2017 04:01:10 Marc via Digitalmars-d-learn wrote:
 how do I from class:
 class Person {

  string name;
  int age;

 }
do:
 auto c = [__traits(allMembers, Person)];
then return only ["name", "age"] rather ["name, "age", "ctor", "toString" ... ]?
Try __traits(derivedMembers, Person). https://dlang.org/spec/traits.html#derivedMembers Depending on what you want though, it's not all that uncommon to use a variety of traits to filter the list down to whatever it is that you actually want. std.traits and std.meta are your friends in addition to __traits. - Jonathan M Davis
Dec 15 2017
parent reply Marc <jckj33 gmail.com> writes:
On Saturday, 16 December 2017 at 07:23:38 UTC, Jonathan M Davis 
wrote:
 On Saturday, December 16, 2017 04:01:10 Marc via 
 Digitalmars-d-learn wrote:
 how do I from class:
 class Person {

  string name;
  int age;

 }
do:
 auto c = [__traits(allMembers, Person)];
then return only ["name", "age"] rather ["name, "age", "ctor", "toString" ... ]?
Try __traits(derivedMembers, Person). https://dlang.org/spec/traits.html#derivedMembers Depending on what you want though, it's not all that uncommon to use a variety of traits to filter the list down to whatever it is that you actually want. std.traits and std.meta are your friends in addition to __traits. - Jonathan M Davis
It derivedMembers worked but I didn't understand how so. It returned the proper array ["name", "age", "this"] but how are them derived? or it's D's design that every class is implicitily derived from a "main objet"? Thanks for your suggeston on std.traits and std.meta, I didn't know about the last one.
Dec 16 2017
parent Jonathan M Davis <newsgroup.d jmdavisprog.com> writes:
On Saturday, December 16, 2017 15:28:46 Marc via Digitalmars-d-learn wrote:
 On Saturday, 16 December 2017 at 07:23:38 UTC, Jonathan M Davis

 wrote:
 On Saturday, December 16, 2017 04:01:10 Marc via

 Digitalmars-d-learn wrote:
 how do I from class:
 class Person {

  string name;
  int age;

 }
do:
 auto c = [__traits(allMembers, Person)];
then return only ["name", "age"] rather ["name, "age", "ctor", "toString" ... ]?
Try __traits(derivedMembers, Person). https://dlang.org/spec/traits.html#derivedMembers Depending on what you want though, it's not all that uncommon to use a variety of traits to filter the list down to whatever it is that you actually want. std.traits and std.meta are your friends in addition to __traits. - Jonathan M Davis
It derivedMembers worked but I didn't understand how so. It returned the proper array ["name", "age", "this"] but how are them derived? or it's D's design that every class is implicitily derived from a "main objet"? Thanks for your suggeston on std.traits and std.meta, I didn't know about the last one.
Every class in D other than Object is derived from another class. So, I assume that the derived in derivedMembers was chosen to indicate that it didn't include any members from base classes. So, it's not that the members are derived from anything; it's that the members come from the derived class. - Jonathan M Davis
Dec 16 2017