Thread overview
Reference Tuples
Oct 09, 2008
dsimcha
Oct 09, 2008
Max Samukha
Oct 09, 2008
BCS
October 09, 2008
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?
October 09, 2008
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.
October 09, 2008
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.