How to get all modules in a package at CT?

Anonymouse asdf at asdf.net
Sat Nov 24 15:21:57 UTC 2018


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.


More information about the Digitalmars-d-learn mailing list