digitalmars.D.learn - How to get all modules in a package at CT?
- Domain (4/4) Nov 24 2018 I have a package named command, and many modules inside it, such
- Anonymouse (23/27) Nov 24 2018 If you just want static if expressions of whether *known* modules
- mw (4/31) Oct 05 2023 How to list unknown sub-modules? (and not walking the source tree
- Neia Neutuladh (9/9) Nov 24 2018 Easiest way is to put this in your build script:
- Andre Pany (17/21) Nov 25 2018 As far as I understand there is s.th. called separate
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.
Nov 24 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.
Nov 24 2018
On Saturday, 24 November 2018 at 15:21:57 UTC, Anonymouse wrote:On Saturday, 24 November 2018 at 08:44:19 UTC, Domain wrote:How to list unknown sub-modules? (and not walking the source tree dir). Is there a compile time solution to this problem? Thanks.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.
Oct 05 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: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.On Saturday, 24 November 2018 at 08:44:19 UTC, Domain wrote:How to list unknown sub-modules? (and not walking the source tree dir). Is there a compile time solution to this problem? Thanks.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.static reflection on import decls is broken, that wont work wellIf 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.
Oct 05 2023
On Thursday, 5 October 2023 at 20:07:38 UTC, user1234 wrote: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.So how about at runtime? I just want the compiler to help to list them, instead of doing manually.static reflection on import decls is broken, that wont work wellIf 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.
Oct 05 2023
On Thursday, 5 October 2023 at 20:42:26 UTC, mw wrote:On Thursday, 5 October 2023 at 20:07:38 UTC, user1234 wrote:I dont have in mind what info are provided by TypeInfoModule but maybe.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.So how about at runtime? I just want the compiler to help to list them, instead of doing manually.static reflection on import decls is broken, that wont work wellIf 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.
Oct 05 2023
On Thursday, 5 October 2023 at 20:42:26 UTC, mw wrote:On Thursday, 5 October 2023 at 20:07:38 UTC, user1234 wrote:At runtime, simply: ```d foreach (m; ModuleInfo) { writeln(m.name); } ``` However, Walter has hinted that he wants to remove ModuleInfo at some point.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.So how about at runtime? I just want the compiler to help to list them, instead of doing manually.static reflection on import decls is broken, that wont work wellIf 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.
Oct 05 2023
On Thursday, 5 October 2023 at 21:25:54 UTC, cc wrote:So ModuleInfo contains all the modules (transitive closure) built into the current binary that is running? Is there document about this ModuleInfo? I only find Struct object.ModuleInfo https://dlang.org/library/object/module_info.html Which seems different.So how about at runtime? I just want the compiler to help to list them, instead of doing manually.At runtime, simply: ```d foreach (m; ModuleInfo) { writeln(m.name); } ``` However, Walter has hinted that he wants to remove ModuleInfo at some point.
Oct 05 2023
On Thursday, 5 October 2023 at 22:32:36 UTC, mw wrote:So ModuleInfo contains all the modules (transitive closure) built into the current binary that is running? Is there document about this ModuleInfo? I only find Struct object.ModuleInfo https://dlang.org/library/object/module_info.html Which seems different.It is opApply() overload on that struct that works with foreach or custom delegate. Since it calls internal stuff there is no point messing up with that. https://github.com/dlang/dmd/blob/d06917a8327027f94b0be0b8e54e47a51ba34134/druntime/src/object.d#L2441
Oct 05 2023
Easiest way is to put this in your build script: find path/to/package -name '*.d' | \ xargs grep '^module ' | \ sed 's,^module,import,' \ > data/modules.d Add `-J data` to your DMD command line, or add `"stringImportPaths": ["data"]` to dub.json. Then in your file: mixin(import("modules.d"));
Nov 24 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.As far as I understand there is s.th. called separate compilations. Your program may is compiled in parts. Your logic which loops over the modules only knows which modules are available at the specific point in time. I do not know whether there is some functionality which tells you all modules available at compile time. But if this functionality exists, you (the developer) has to be aware of separate compilation and should have a plan how to mitigate. Either do not use separate compilation or always force recompilation of all source code files... In would really like to have such functionality and also the possibility to express in code: always recompile this part of code (the loop over the modules) even in separate compilation. I do not know whether that is technically possible... Kind regards Andre
Nov 25 2018