How to find all modules in a package?
H. S. Teoh
hsteoh at qfbox.info
Thu Aug 4 10:14:00 UTC 2022
On Thu, Aug 04, 2022 at 07:22:04AM +0000, Domain via Digitalmars-d-learn wrote:
> On Wednesday, 3 August 2022 at 12:27:32 UTC, frame wrote:
> > On Wednesday, 3 August 2022 at 03:36:55 UTC, Domain wrote:
> > > I want to find out all public functions in all modules in a package.
> > > Can I do that at compile time?
> >
> > You can do something like that:
> >
> > ```d
> > static foreach (sym; __traits(allMembers, mixin("std.string")))
> > {
> > pragma(msg, sym.stringof);
> > }
> > ```
> >
> > Then you would have to check if `sym` is a template or function or
> > something else.
>
> This give me all symbols in the package.d file.
static foreach (sym; __traits(allMembers, mixin("std.string")))
{
// Note that sym is already a string, there is no need to use .stringof
static if (is(typeof(mixin(sym)) == function))
pragma(msg, sym);
}
Note, however, that this doesn't pick up on template functions, only
non-template global functions. You can detect templates with `static if
(__traits(isTemplate, mixin(sym)))`, but in general there is no way to
know whether it's a template function, because in order to introspect it
you have to instantiate it first, but there is no general way to
automatically instantiate a template. Its arguments can be subject to
arbitrary signature constraints, and it may in theory be a template
function only for a subset of its possible arguments, so there isn't any
good way to automatically deduce what template arguments would
instantiate into a valid function. You can only get at a template
function if you already have an instantiation of it.
T
--
Guns don't kill people. Bullets do.
More information about the Digitalmars-d-learn
mailing list