www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Protection attribute in another module

reply jmh530 <john.michael.hall gmail.com> writes:
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));
     }
}
Aug 28
parent reply Kagamin <spam here.lot> writes:
You iterate over string literals: 
https://dlang.org/spec/traits.html#allMembers
Aug 29
parent reply jmh530 <john.michael.hall gmail.com> writes:
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)); } } }
Aug 29
parent reply Kagamin <spam here.lot> writes:
Something like mixin("__traits(getProtection, A."~member~")")
Aug 30
parent jmh530 <john.michael.hall gmail.com> writes:
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"); }
Aug 30