Thread overview
Protection attribute in another module
Aug 28, 2017
jmh530
Aug 29, 2017
Kagamin
Aug 29, 2017
jmh530
Aug 30, 2017
Kagamin
Aug 30, 2017
jmh530
August 28, 2017
How do I get the protection status of function in another module?

Basically I have some code that loops through all the members of another module and I want to be able to skip the ones that are private.

The code below prints public for foo.

module A;

private void foo();

--------------------------------

module B;

import A;

void main()
{
    import std.stdio : writeln;

    foreach(member; __traits(allMembers, A))
    {
        writeln(member);
        writeln(__traits(getProtection, member));
    }
}
August 29, 2017
You iterate over string literals: https://dlang.org/spec/traits.html#allMembers
August 29, 2017
On Tuesday, 29 August 2017 at 15:48:05 UTC, Kagamin wrote:
> You iterate over string literals: https://dlang.org/spec/traits.html#allMembers

I had known that, but I couldn't figure out how to get the identifiers. It turns out that I can loop over getOverloads and it produces the desired result (below).

module B;

import A;

void main()
{
    import std.stdio : writeln;

    foreach(member; __traits(allMembers, A))
    {
        foreach (t; __traits(getOverloads, A, member))
        {
            writeln(__traits(getProtection, t));
        }
    }
}
August 30, 2017
Something like mixin("__traits(getProtection, A."~member~")")
August 30, 2017
On Wednesday, 30 August 2017 at 21:15:56 UTC, Kagamin wrote:
> Something like mixin("__traits(getProtection, A."~member~")")

The following compiles without error. It would be nice if something like this got added to std.traits.

template getProtection(string from, string member)
{
    mixin("static import " ~ from ~ ";");
    enum string getProtection =
                  mixin("__traits(getProtection, " ~ from ~ "." ~ member ~ ")");
}

@safe unittest
{
    assert(getProtection!("std.algorithm", "map") == "public");
}