On Tuesday, 11 July 2017 at 21:23:28 UTC, Ali Çehreli wrote:
Default template and function arguments are resolved at
instantiation site, which means __MODULE__ would resolve
automatically to the caller's module. For example, if you have
this module:
__MODULE__ is a string so I cannot pass it to
__traits(allMembers). I worked around it via a string mixin that
creates an alias for the module:
template RegisterMethods()
{
import std.array;
mixin("alias MODULE = " ~ __MODULE__ ~ ";");
auto RegisterMethods() {
string[] s;
foreach (m; __traits(allMembers, MODULE)) {
// do stuff
}
return join(s, "");
}
}
mixin(RegisterMethods);
I wonder if there is a better way.
Also, I am using the outer string mixin (as opposed to a plain
mixin) because I will need to produce a series of aliases:
mixin(RegisterMethods);
// injects:
// alias add = Method!("add", Matrix, virtual!Matrix,
virtual!Matrix).dispatcher
// alias add = Method!("add", Matrix, virtual!Matrix,
virtual!Matrix).discriminator;
// alias kick = Method!("kick", Animal).dispatcher
// alias kick = Method!("kick", Animal).discriminator
// etc
The only way I can see is with a string mixin.
I thought about the new syntax since yesterday, and hit some
problems and some solutions. At this point maybe we should
continue that discussion elsewhere than the Learn forum...