Store struct tuple of alias and access members through it?

Timoses timosesu at gmail.com
Sat Apr 7 13:31:01 UTC 2018


(Please read at the very bottom what I'd like to achieve)

Is it possible to return the member of a struct by its .tupleof 
index?

I know that it would work on a struct value, but I'd like it to 
work on the type's tupleof:

```
struct S { int i;}
S s;
// below leads to: Error: need this for s1 of type uint
// writeln(/*somehow access s via the S tupleof? */ S.tupleof[0]);
// vs
writeln(s.tupleof[0]);
```

See below example to make the intention a bit clearer:

https://run.dlang.io/gist/6fdb01ddd78b14f8b9a94ac951580cb8
```
struct S
{
     uint s1;
     ushort s2;
}

interface IParam
{}

template Param(T)
{
     static if (isBasicType!T)
         alias members = AliasSeq!();
     else
         alias members = AliasSeq!(T.tupleof);

     class Param : IParam
     {
         T m;
         this(T m)
         {
             this.m = m;
         }

         IParam opIndex(size_t i)
         {
             // Something like this possible?????
             // return this.m.members[i];       // <------------ 
how???

             // This works but feels needless.
             static foreach (j, t; members)
                 if (i == j)
             	{
                 	return new 
Param!(typeof(members[j]))(__traits(getMember, this.m, 
members[j].stringof));          // <------------- 
members[j].stringof feels ugly just to get the member that should 
be stored in 'members' already...
            		}
             return null;
         }
     }
}
```

The reason why I don't want `m.tupleof[i]` is because later I'd 
like to consider bitfields within the struct. This means I'd have 
to also consider the member functions of the struct and 
potentially return them.

E.g.

```
struct S
{
     int s1;
     int s2() { return 3; }
}
```

and then I'd like to have
alias members = (s1, s2) // pseudo code..

so I could return
S s;
s.members[1]; // would evaluate the function s2 and return the 
value

----------

In the end I would like to accomplish the following:
Provide access to contained bitfields and members of a struct in 
the order they
appear in the struct via an index.

I hope I made a somewhat decent job in explaining what I'm trying 
to accomplish.

Please let me know if anything is unclear.


More information about the Digitalmars-d-learn mailing list