Getting the current module as a symbol
Mithun Hunsur via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Tue Apr 12 19:50:58 PDT 2016
On Tuesday, 12 April 2016 at 14:22:22 UTC, Adam D. Ruppe wrote:
> On Tuesday, 12 April 2016 at 13:44:07 UTC, Mithun Hunsur wrote:
>> I'm looking for the equivalent of `typeof(this)` in module
>> scope (so that it gets the current module).
>
> The trick I use is `mixin(__MODULE__)`.
>
> I also mentioned this in my book
>
> https://www.packtpub.com/application-development/d-cookbook
>
> the free sample is the reflection chapter which has a few
> tricks like this.
Thanks! That works great. For anyone else stumbling across this
in the future: if your mixin is in another file, you need to pass
in __MODULE__ as a default argument:
mixin template ATemplate(string moduleName = __module__)
{
mixin(moduleName); // Is a reference to the module that
instantiated it.
}
This can be a little finicky, so I'd still like to see
language-level support for getting the current module (perhaps
defining 'this' at module-level to be the current module). I'll
probably make a post in the main group for it - thanks for the
help :)
More information about the Digitalmars-d-learn
mailing list