stringof and mixins when the types aren't available
Adam D. Ruppe
destructionator at gmail.com
Sun Jun 16 17:56:08 PDT 2013
On Monday, 17 June 2013 at 00:27:07 UTC, David Nadlinger wrote:
> The fix is simple: Replace `mixin(T.stringof ~ ".init")` by
> `return mixin("T.init")`.
Eh that doesn't seem to work in a more complex case... here's
something that tries to implement an interface with stubs. The
hard part is the arguments of the members:
====
module a;
auto magic(Interface)() {
import std.traits;
template passthrough(T) { alias passthrough = T; }
string makeCode() {
string code = "class Class : Interface {";
foreach(memIdx, member; __traits(allMembers,
Interface)) {
import std.conv;
string args;
foreach(idx, arg;
ParameterTypeTuple!(__traits(getMember, Interface, member))) {
if(idx) args ~= ",";
//args ~= arg.stringof ~ " arg" ~
to!string(idx);
// trying to use local names
args ~=
"passthrough!(ParameterTypeTuple!(__traits(getMember, Interface,
\""~member~"\"))[" ~ to!string(idx) ~ "]) arg" ~ to!
string(idx);
}
code ~= "override
ReturnType!(__traits(getMember, Interface, \""~member~"\")) " ~
member ~ "(" ~ args ~ ") {}";
}
code ~= "}";
return code;
}
pragma(msg, makeCode());
mixin(makeCode());
return new Class();
}
=====
module b;
struct S{};
interface Test { void foo(S s); }
void main() {
import a;
auto s = magic!Test();
}
===
The stringof doesn't work because the name is unknown. But the
longer one, currently uncommented, doesn't seem to work either.
dmd just dumps a whole lot of trash errors referencing phobos and
nothing specifically about this code... eyeballing it, I don't
think I screwed it up though.
Is this the right principle at least?
More information about the Digitalmars-d
mailing list