How do you print or capture the current module name?

Viewed 111

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

2 Answers

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__).

Using __MODULE__ is the way to go. It's a special token, like __FILE__ and __LINE__, which means it'll get expanded at the call site. See the specs for some example.

Related