D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 20648 - static foreach over allMembers of module doesn't seem to work
Summary: static foreach over allMembers of module doesn't seem to work
Status: RESOLVED INVALID
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P1 major
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2020-03-09 03:44 UTC by Manu
Modified: 2020-03-21 03:56 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Manu 2020-03-09 03:44:42 UTC
This should list all the structs in the module:

----------------------
module my_module;

struct Wow {}

template ScrapeStructs(Tys...)
{
    import std.meta : AliasSeq;
    static if (Tys.length == 0)
        alias ScrapeStructs = AliasSeq!();
    else static if (is(Tys[0] == struct))
        alias ScrapeStructs = AliasSeq!(Tys[0], ScrapeStructs!(Tys[1 .. $]));
    else
        alias ScrapeStructs = ScrapeStructs!(Tys[1 .. $]);
}

alias AllStructs = ScrapeStructs!(__traits(allMembers, my_module));

pragma(msg, AllStructs); // ()
pragma(msg, is(Wow == struct)); // true
----------------------

But it doesn't...
Comment 1 Boris Carvajal 2020-03-09 08:37:20 UTC
But "__traits(allMembers" returns a tuple of strings.
You can use mixin to get a type (2.088+) or getMember.
---
else static if (is(mixin(Tys[0]) == struct))
        alias ScrapeStructs = AliasSeq!(mixin(Tys[0]), ScrapeStructs!(Tys[1 .. $]));
---
Comment 2 Manu 2020-03-09 09:50:14 UTC
Oh yeah, ffs... Sorry! Stupid mistake! >_<