How to write template ModuleOf!T ?
kenji hara
k.hara.pg at gmail.com
Wed Dec 15 00:59:48 PST 2010
Hello, all.
I tried to ModuleOf!T in some day. It may works like this:
----
module mod.a;
struct S{}
struct Nest{
struct S{}
}
module test;
import mod.a;
static assert(ModuleOf!(S) == "mod.a");
static assert(ModuleOf!(Nest.S) == "mod.a");
----
Partially I succeed to get module name of mod.a.S by using .mangeof property.
But sadly failed to get of mod.a.Nest.S.
I think that D should support __traits(isModuleName, ModuleName).
Do you think?
P.S.
The original motivation is reproduction ADL in C++.
Currently in D, we cannot call function f specialized by S in following code:
----
module a;
struct S{...}
void f(S s){...} // specialized implementation
module b;
void f(T)(T t){...} // generic implementation
void someAlgorithm(T)(T t){
f(t)
}
----
module test;
import a, b;
void main(){
S s;
someAlgrithm(s);
}
----
But, if ModuleOf exists, we can call specialized f to write
someAlgorithm as follows:
----
template ScopeOf(T)
{
mixin("import ScopeOf = " ~ ModuleOf!T ~ ";");
}
void someAlgorithm(T)(T t){
static if (is(typeof(ScopeOf!T.f)))
alias ScopeOf!T.f f;
else
alias .f f;
f(s); // call function specialized by T or generic one.
}
----
This is like ADL in C++, but has no implicit problem.
Thanks for your reading.
Kneji
More information about the Digitalmars-d
mailing list