Thread overview
How to find all modules in a package?
Aug 03, 2022
Domain
Aug 03, 2022
Piotr Mitana
Aug 03, 2022
Adam D Ruppe
Aug 03, 2022
frame
Aug 04, 2022
Domain
Aug 04, 2022
H. S. Teoh
August 03, 2022

I want to find out all public functions in all modules in a package. Can I do that at compile time?

August 03, 2022

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?

I think it's not possible.

August 03, 2022
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?

No, D packages are not closed; anyone can add new modules to them at any time.
August 03, 2022

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:

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.

August 04, 2022

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:

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.

August 04, 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.