Reputation: 7061
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
Upvotes: 2
Views: 270
Reputation: 893
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.
Upvotes: 2
Reputation: 58
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__)
.
Upvotes: 3