On Friday, 7 July 2023 at 17:46:09 UTC, Cecil Ward wrote:
A bit of a weird question, and I’m not sure how to word it. Say I have a module, and I’d like to list / enumerate all the public visible things that the module exports / publishes ‘ makes visible. Is there a way of doing that ? Of getting that kind of listing?

I’m wondering about information leaking when things should be encapsulated.

I did this. It's super ugly and even has `__traits(compiles)` in there, but as a quick and dirty solution it served well enough.

```d
void printPublicMembersOfModule(string module_)()
{
    mixin("import thisModule = " ~ module_ ~ ";");

    foreach (symstring; __traits(allMembers, thisModule))
    {
        alias symbol = __traits(getMember, thisModule, symstring);
        static if (
            __traits(compiles, __traits(getVisibility, symbol)) &&
            __traits(getVisibility, symbol) == "public")
        {
            pragma(msg, symstring);
        }
    }
}

void main()
{
    printPublicMembersOfModule!"std.stdio"();
}
```

https://run.dlang.io/is/tvNDdp

Reply via email to