Thread overview |
---|
August 28, 2017 Protection attribute in another module | ||||
---|---|---|---|---|
| ||||
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 Re: Protection attribute in another module | ||||
---|---|---|---|---|
| ||||
Posted in reply to jmh530 | You iterate over string literals: https://dlang.org/spec/traits.html#allMembers |
August 29, 2017 Re: Protection attribute in another module | ||||
---|---|---|---|---|
| ||||
Posted in reply to Kagamin | 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 Re: Protection attribute in another module | ||||
---|---|---|---|---|
| ||||
Posted in reply to jmh530 | Something like mixin("__traits(getProtection, A."~member~")") |
August 30, 2017 Re: Protection attribute in another module | ||||
---|---|---|---|---|
| ||||
Posted in reply to Kagamin | 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"); } |
Copyright © 1999-2021 by the D Language Foundation