Thread overview
Public visible entities published by a module
Jul 07, 2023
Cecil Ward
Jul 07, 2023
Anonymouse
Jul 07, 2023
Cecil Ward
July 07, 2023
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.
July 07, 2023

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.

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

July 07, 2023
On Friday, 7 July 2023 at 19:49:06 UTC, Anonymouse wrote:
> On Friday, 7 July 2023 at 17:46:09 UTC, Cecil Ward wrote:
>> [...]
>
> 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

Wow, thankyou so much for your generous reply.