Scooter
Scooter

Reputation: 7061

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

Upvotes: 2

Views: 270

Answers (2)

Geod24
Geod24

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

Jonas Meeuws
Jonas Meeuws

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

Related Questions