On Wed, Sep 1, 2010 at 15:25, Simen kjaeraas <simen.kja...@gmail.com> wrote:
> Philippe Sigaud <philippe.sig...@gmail.com> wrote: > > I also have a template that gets the list of all members of an aggregate >> type (classes, structs and, well, modules, in a way), even the >> constructors, >> even the overloaded members, separated by type, and list them with their >> names and associated type. It even gets the types aliases and unittests, >> though I don't know what to do with the latter. That was part of a project >> to duplicate a type: generate a new type with this extracted info. I had >> vague projects to transform the methods into their const equivalent for >> example or to render the fields immutable, but didn't get that far. Would >> that be interesting to someone? >> > > I could imagine extracting the unittests could be useful for making a > unittest system. Are they only included when passing -unittest? > Hmm, don't know. Let see: import std.stdio; class C { int i; unittest { assert(true); writeln("Hello World!"); } } void main() { writeln([__traits(allMembers, C)]); writeln(typeof(C.__unittest1()).stringof); C.__unittest1(); } No, compile this with and without -unittest, and __unittest1 is present. Oh, btw, contrary to the docs __traits(getOverloads, T) works also for structs and modules, not only classes. The unittests type is void delegate(), which is coherent with them being blocks of instructions. You can even call it like a function (see the last line of main). Strange, I never could get that to work... Maybe I didn't try this with a class. Philippe