www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Reference Tuples

reply dsimcha <dsimcha yahoo.com> writes:
I'm trying to do some deep metaprogramming magic in D2, and an issue that I
can't seem to get around is that I can't find a way to get a tuple of
struct/class fields, or something similarly useful, with reference semantics. 
Ex:

struct S {
    uint foo = 1;
}

void main() {
    S s;
    foreach(element; s.tupleof)
         element = 2;
    writeln(s.foo);  //Still 1.
}

The obvious thing, which I already tried was:

foreach(ref element; s.tupleof)  //Doesn't compile.

Is there a way to get a tuple representation of a class/struct such that I can
change the fields of the class/struct through the tuple representation, or
should I file this as an enhancement?
Oct 08 2008
parent reply Max Samukha <samukha voliacable.com.removethis> writes:
On Thu, 9 Oct 2008 01:49:36 +0000 (UTC), dsimcha <dsimcha yahoo.com>
wrote:

I'm trying to do some deep metaprogramming magic in D2, and an issue that I
can't seem to get around is that I can't find a way to get a tuple of
struct/class fields, or something similarly useful, with reference semantics. 
Ex:

struct S {
    uint foo = 1;
}

void main() {
    S s;
    foreach(element; s.tupleof)
         element = 2;
    writeln(s.foo);  //Still 1.
}

The obvious thing, which I already tried was:

foreach(ref element; s.tupleof)  //Doesn't compile.

Is there a way to get a tuple representation of a class/struct such that I can
change the fields of the class/struct through the tuple representation, or
should I file this as an enhancement?
Possible workaround: struct S { uint foo = 1; } void main() { S s; foreach(i, element; s.tupleof) s.tupleof[i] = 2; writeln(s.foo); } But you file it as enhancement anyway.
Oct 08 2008
parent BCS <ao pathlink.com> writes:
Reply to Max,

 struct S {
 uint foo = 1;
 }
 void main() {
 S s;
 foreach(i, element; s.tupleof)
 s.tupleof[i] = 2;
 writeln(s.foo);
 }
That's what I have ended up doing every time I have needed this.
Oct 09 2008