Search code examples
d

How do you print or capture the current module name?


I want to print the module name of my main source file in that source file. I tried this:

import std.stdio;
import std.traits; // moduleName template

int main(string[] args)
{
    writeln("The module name is: ",moduleName!moduleName);
    return 0;
}

However, it prints:

The module name is: std.traits


Solution

  • The moduleName template description is:

    Get the module name (including package) for the given symbol.

    So moduleName!moduleName should give std.traits.

    Just replace the template argument with any other symbol to get it's module name.

    For example writeln(moduleName!main).

    Another way is using writeln(__MODULE__).