Getting the names of all the top-level functions in module

Adam D Ruppe destructionator at gmail.com
Tue Sep 21 14:20:50 UTC 2021


On Tuesday, 21 September 2021 at 13:29:44 UTC, Stefan Koch wrote:
> Note that on the "callsite" you have to instantiate the helper 
> template as:

Not true. You can pass module names as plain names now, no more 
need for the special mixin, with the exception of getting the 
current module (just because __MODULE__ is a string), but if this 
were a library function, you wouldn't be using the current module 
anyway.

> whereas the template above used 5 magic constructs.
>  - 2 different types of pattern matching is expressions.
>    and
>  - 3 different __traits expressions.

Not true. There's two extremely basic traits and one is 
expression actually necessary here. You overcomplicated it again.


This works today:

import arsd.simpledisplay; // or whatever

enum string[] functionNames(alias M) = ()
{
         string[] names;
         foreach(memberName; __traits(derivedMembers, M))
             static if (is(typeof(__traits(getMember, M, 
memberName)) ==
             function))
                 names ~= memberName;
         return names;
}();

pragma(msg, functionNames!(arsd.simpledisplay));



As you can see, there's one trait, and one is expression, to see 
if it is a function. Not that complicated.


> you have to use it directly you cannot assign it to a module 
> level variable.

Also not true.

---
module qwe.whatever;

enum string[] functionNames(alias M) = ()
{
         string[] names;
         foreach(memberName; __traits(derivedMembers, M))
             static if (is(typeof(__traits(getMember, M, 
memberName)) == function))
                 names ~= memberName;
         return names;
}();

const fNames = functionNames!(qwe.whatever);

void main() {
         import std.stdio;
         writeln(fNames);
}

void foo() {}
---



$ dmd qwe
$ ./qwe
["main", "foo"]
$ nm qwe.o | grep functionNames
// notice this is empty btw
$


More information about the Digitalmars-d mailing list