digitalmars.D - How to write template ModuleOf!T ?
- kenji hara (55/55) Dec 15 2010 Hello, all.
- Simen kjaeraas (8/10) Dec 15 2010 A way to traverse the hierarchy of identifiers would be nice.
- kenji hara (5/16) Dec 15 2010 Even if we can use is(T == module), T should be valid identifier.
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
Dec 15 2010
kenji hara <k.hara.pg gmail.com> wrote:I think that D should support __traits(isModuleName, ModuleName). Do you think?A way to traverse the hierarchy of identifiers would be nice. There is currently no way to identify an identifier as a template or module. I would say is( T == module ) and is( T == template ) is the way to go, but I'm not entirely sure (they're not types, which is what is() deals in). -- Simen
Dec 15 2010
2010/12/15 Simen kjaeraas <simen.kjaras gmail.com>:kenji hara <k.hara.pg gmail.com> wrote:Even if we can use is(T == module), T should be valid identifier. Then, we should treat the module name yet not imported as a string. Therefore, Is-expression will not fit this. Thanks.I think that D should support __traits(isModuleName, ModuleName). Do you think?A way to traverse the hierarchy of identifiers would be nice. There is currently no way to identify an identifier as a template or module. I would say is( T == module ) and is( T == template ) is the way to go, but I'm not entirely sure (they're not types, which is what is() deals in). -- Simen
Dec 15 2010