[Issue 13079] Need 'this' to access member - function literal

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Mon Jul 14 15:24:36 PDT 2014


https://issues.dlang.org/show_bug.cgi?id=13079

--- Comment #8 from hsteoh at quickfur.ath.cx ---
Ahh, I see. So you're basically using mixin as a macro system for generating
code inside a single class. Makes sense.

The following code works for me (dmd git HEAD):
----
import std.string;
class C {
        mixin({
                string code="";
                foreach (i; 0..10) {
                        code ~= format("int x%d;\n", i);
                }
                return code;
        }());
}
void main() {
        foreach (memb; __traits(allMembers, C)) {
                static if (is(typeof(__traits(getMember, C.init, memb)) T))
                {
                        pragma(msg, memb);
                }
        }
}
----
So obviously, it's possible to use a code block to generate declarations inside
the class, at least on dmd git HEAD. You should be able to adapt this code to
your needs. (The code in main() is just to prove that those declarations
actually got inserted, you can ignore it.)

Which version of dmd are you using?

--


More information about the Digitalmars-d-bugs mailing list