How to get all modules in a package at CT?

user1234 user1234 at 12.de
Thu Oct 5 20:07:38 UTC 2023


On Thursday, 5 October 2023 at 18:40:36 UTC, mw wrote:
> On Saturday, 24 November 2018 at 15:21:57 UTC, Anonymouse wrote:
>> On Saturday, 24 November 2018 at 08:44:19 UTC, Domain wrote:
>>> I have a package named command, and many modules inside it, 
>>> such as command.build, command.pack, command.help...
>>> I want to get all these modules at compile time so that I 
>>> know what command is available.
>>
>> If you just want static if expressions of whether *known* 
>> modules are available or not, then test if 
>> __traits(identifier, package.module) compiles.
>>
>> ---
>>
>> // Two-step workaround for 
>> https://issues.dlang.org/show_bug.cgi?id=19409
>> enum hasBuild = __traits(compiles, __traits(identifier, 
>> command.build));
>> enum hasPack = __traits(compiles, __traits(identifier, 
>> command.pack));
>> enum hasHelp = __traits(compiles, __traits(identifier, 
>> command.help));
>>
>> static if (hasBuild) { /* ... */ }
>> static if (hasPack) { /* ... */ }
>> static if (hasHelp) { /* ... */ }
>>
>> ---
>>
>> __traits(compiles, { import package.module; }) mostly works, 
>> but I ran into problems when the module was available and 
>> merely did not compile.
>>
>> If you want to iterate the package for modules imported in it, 
>> I'm not sure. __traits(allMembers, package) will list names of 
>> imported packages but not which modules.
>
>
> How to list unknown sub-modules? (and not walking the source 
> tree dir). Is there a compile time solution to this problem?
>
> Thanks.

No. Sorry.

Generally compile time code cannot interact with the system. To 
be evaluable at compile time code has to be strongly pure, that 
is not the case of the function you would need.

Otherwise you'd need a new traits for that... but that traits 
would violate the rule explained before.

>> If you want to iterate the package for modules imported in it, 
>> I'm not sure. __traits(allMembers, package) will list names of 
>> imported packages but not which modules.

static reflection on import decls is broken, that wont work well


More information about the Digitalmars-d-learn mailing list