www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How do you find the struct types in a module? - getting Error: unknown

reply aliak <something something.com> writes:
Hi,

I'm trying to get a typed list of structs in my current module 
that matches certain criteria. Something like:

module mod;

struct X {}
struct Y {}

alias allTypes = ListOfTypes!mod;

But I'm having a bit of trouble. I have this so far:

template ListOfTypes(alias T) {
     import std.meta;
     enum isStruct(string m) = is(mixin(m) == struct);
     enum types = Filter!(isStruct, __traits(allMembers, T));
     alias toType(string m) = mixin(m);
     alias all = staticMap!(toType, types);
     alias CommandGroupsOf = all;
}

pragma(msg, ListOfTypes!mod);

But that causes an error I've never seen before:

Error: unknown, please file report on issues.dlang.org
onlineapp.d(30,1):        while evaluating `pragma(msg, 
CommandGroupsOf!(mod))`


Any workarounds or maybe insight in to the error?
Feb 13 2020
parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
On 2/13/20 9:47 AM, aliak wrote:
 Hi,
 
 I'm trying to get a typed list of structs in my current module that 
 matches certain criteria. Something like:
 
 module mod;
 
 struct X {}
 struct Y {}
 
 alias allTypes = ListOfTypes!mod;
 
 But I'm having a bit of trouble. I have this so far:
 
 template ListOfTypes(alias T) {
      import std.meta;
      enum isStruct(string m) = is(mixin(m) == struct);
      enum types = Filter!(isStruct, __traits(allMembers, T));
      alias toType(string m) = mixin(m);
      alias all = staticMap!(toType, types);
      alias CommandGroupsOf = all;
 }
 
 pragma(msg, ListOfTypes!mod);
 
 But that causes an error I've never seen before:
 
 Error: unknown, please file report on issues.dlang.org
 onlineapp.d(30,1):        while evaluating `pragma(msg, 
 CommandGroupsOf!(mod))`
 
 
 Any workarounds or maybe insight in to the error?
 
Not sure about your error, but here is a working version (don't use mixins, use __traits(getMember)): import std.meta; template ListOfStructs(alias mod) { enum isStruct(string m) = is(__traits(getMember, mod, m) == struct); alias getMember(string m) = __traits(getMember, mod, m); alias ListOfStructs = staticMap!(getMember, Filter!(isStruct, __traits(allMembers, mod))); } -Steve
Feb 13 2020
parent aliak <something something.com> writes:
On Thursday, 13 February 2020 at 15:38:37 UTC, Steven 
Schveighoffer wrote:
 On 2/13/20 9:47 AM, aliak wrote:
[...]
Not sure about your error, but here is a working version (don't use mixins, use __traits(getMember)): import std.meta; template ListOfStructs(alias mod) { enum isStruct(string m) = is(__traits(getMember, mod, m) == struct); alias getMember(string m) = __traits(getMember, mod, m); alias ListOfStructs = staticMap!(getMember, Filter!(isStruct, __traits(allMembers, mod))); } -Steve
It works without the mixin indeed! Thank you!
Feb 13 2020