www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Public visible entities published by a module

reply Cecil Ward <cecil cecilward.com> writes:
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.
Jul 07 2023
parent reply Anonymouse <zorael gmail.com> writes:
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
Jul 07 2023
parent Cecil Ward <cecil cecilward.com> writes:
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.
Jul 07 2023