On Friday, 28 November 2014 at 19:24:33 UTC, Daniel Kozak via
Digitalmars-d wrote:
Dne Fri, 28 Nov 2014 20:01:41 +0100 bitwise via Digitalmars-d
<[email protected]> napsal(a):
The docs for template mixins do show mixins inside functions,
so is this a bug, or is there something else I'm doing wrong?
//**************************
module main;
import std.traits;
class ModInfo {
}
mixin template moduleInfo(alias MODULE) {
static const(ModInfo) __module_info = new ModInfo;
}
const(ModInfo) getModuleInfo(alias mod)() {
static if(__traits(hasMember, mod, "__module_info")) {
return __traits(getMember, mod, "__module_info");
} else {
mixin moduleInfo!mod; // ERROR [1]
return __module_info;
}
}
void main() {
static const(ModInfo) info = getModuleInfo!(main);
}
//**************************
[1] main.d(17,3): Error: Declaration mixin moduleInfo!(main);
is not yet implemented in CTFE
main.d(23,31): called from here: getModuleInfo()
module main;
immutable class ModInfo {
}
static moduleInfo(alias MODULE)() {
return new ModInfo();
}
static getModuleInfo(alias mod)() {
static if(__traits(hasMember, mod, "__module_info")) {
return __traits(getMember, mod, "__module_info");
} else {
return moduleInfo!mod;
}
}
void main() {
immutable info = getModuleInfo!(main);
}
Thanks, but this still doesn't fix my problem. The idea is that
getModuleInfo should return the same static copy of ModInfo at
each call to avoid duplication. ModInfo is a recursive reflection
of an entire module, so it could be huge.
My goal is that someone could create a global (ModInfo
__module_info) object inside their own module through a mixin
template, which would allow reflection of private members in that
module. If getModuleInfo() was called for a module that was
missing __ module_info, a static copy would be created inside
getModuleInfo(). This would be the case if someone wanted to
reflect a static library that they themselves did not compile.