www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Is __traits(derivedMembers) supposed to return mangled names?

reply Mike Hearn <mike plan99.net> writes:
The language spec is ambiguous, and DMD can apparently do either. This program
prints

[a __T3TagS23_D11smartstruct4Test1aiVi1Z b
__T3TagS24_D11smartstruct4Test1bAaVi2Z]

Note: this is a lame-ass hack around the lack of real metadata attributes in
D2. It'd be nice if the real thing was supported.


import std.stdio;

template Tag(alias m, int i) {
  mixin("const int tag_" ~ m.stringof ~ " = " ~ i.stringof ~ ";");
};

class Test {
  int    a;   mixin Tag!(a, 1);
  char[] b;   mixin Tag!(b, 2);
};

int main(char[][] args) {
  writefln(__traits(derivedMembers, Test));
  return 0;
}
Dec 05 2008
parent reply "Denis Koroskin" <2korden gmail.com> writes:
On Sat, 06 Dec 2008 00:13:40 +0300, Mike Hearn <mike plan99.net> wrote:

 The language spec is ambiguous, and DMD can apparently do either. This  
 program prints

 [a __T3TagS23_D11smartstruct4Test1aiVi1Z b  
 __T3TagS24_D11smartstruct4Test1bAaVi2Z]

 Note: this is a lame-ass hack around the lack of real metadata  
 attributes in D2. It'd be nice if the real thing was supported.


 import std.stdio;

 template Tag(alias m, int i) {
   mixin("const int tag_" ~ m.stringof ~ " = " ~ i.stringof ~ ";");
 };

 class Test {
   int    a;   mixin Tag!(a, 1);
   char[] b;   mixin Tag!(b, 2);
 };

 int main(char[][] args) {
   writefln(__traits(derivedMembers, Test));
   return 0;
 }

Yeah, I'm using the same hack (i.e. defining private static const members) to denote some members characteristics, e.g. whether a member is serializable, its value is written to log etc.
Dec 05 2008
parent Mike Hearn <mike plan99.net> writes:
 Yeah, I'm using the same hack (i.e. defining private static const members)  
 to denote some members characteristics, e.g. whether a member is  
 serializable, its value is written to log etc.

Is the code open source? I was intending to put together some kind of better integrated protobufs equivalent, ie with the same concept of optional/required tagged fields and efficient serialization, but type safe and better integrated with the language. Another oddity of __traits, this prints dump! a __T3TagS23_D11smartstruct4Test1aiVi1Z b __T3TagS24_D11smartstruct4Test1bAaVi2Z __T11SmartStructZ So dump() isn't considered a member, but the name of the mixin does appear! import std.stdio; template Tag(alias m, int i) { mixin("const int tag_" ~ m.stringof ~ " = " ~ i.stringof ~ ";"); }; template SmartStruct() { void dump() { writefln("dump!"); } }; struct Test { int a; mixin Tag!(a, 1); char[] b; mixin Tag!(b, 2); mixin SmartStruct; }; int main(char[][] args) { Test t; t.dump(); foreach (member; __traits(allMembers, Test)) { writefln(member); } return 0; }
Dec 05 2008