static code generation
r_m_r
r_m_r at mailinator.com
Thu Dec 20 21:00:00 PST 2012
On 12/21/2012 07:18 AM, js.mdnq wrote:
> struct S_A { int x; int y; void func(); void funcA(); }
> struct S_B { int x; int z; void func(); void funcB(); }
>
> then GenUnion(S_A, S_B) will produce a struct like
> S_AB
> {
> int x;
> int y;
int z; //did u miss this? ;)
> void func();
> void funcA();
> void funcB();
> }
check this: http://pastebin.com/BQW7DtSD
struct S_A { int x; int y; void func() { x = 2*x; }; void funcA() { }; }
struct S_B { int x; int z; void func() { x = 3*x; }; void funcB() { }; }
void main()
{
import std.stdio: writeln;
mixin (Gen!("S_AB", S_A, S_B));
pragma(msg, __traits(allMembers, S_AB));
//prints: tuple("_s0","_s1","x","y","func","funcA","z","funcB")
S_AB s_ab;
s_ab.x = 10;
s_ab.func();
assert(s_ab.x == 20);
}
BTW what you are trying to achieve with your GenUnion(S_A, S_B) may
require functionality currently missing in D (as you've already
mentioned). For example, I can generate a struct with the fields from
the two structs S_A and S_B like so:
struct S_AB
{
int x;
int y;
int z;
void func() { } ; //note the empty function body
void funcA() { } ;
void funcB() { };
}
but it will be useless since AFAIK I cannot get a stringof S_A.func()'s
body at compile time.
One alternative as I've shown is to generate a struct with hidden fields
and properties (until 'multiple alias this' functionality gets
implemented) which will atleast behave like your GenUnion(S_A, S_B)
struct (from a user perspective).
BTW if you find any interesting alternative, please consider adding it
to the dwiki - http://wiki.dlang.org/ (for e.g.:
http://wiki.dlang.org/Combining_Structs)
regards,
r_m_r
More information about the Digitalmars-d-learn
mailing list