That's probably not the solution you want, but could you use direct string mixins? import std.stdio; string B() { return `void foo(double d) { writeln("foo(double)"); }`; } class C { void foo(int x) { writeln("x"); } mixin(B()); } void main() { auto c = new C(); c.foo(1.3); }