compile-time opIndex

H. S. Teoh via Digitalmars-d digitalmars-d at puremagic.com
Wed Dec 31 13:35:24 PST 2014


On Wed, Dec 31, 2014 at 10:34:07PM +0100, Artur Skawina via Digitalmars-d wrote:
> On 12/18/14 16:43, Steven Schveighoffer via Digitalmars-d wrote:
> > We currently have the ability to do opIndex for simulating an array
> > or other type indexed at runtime.
> > 
> > But we have no way to simulate the ability of tuple indexing. Such
> > an ability would unleash a huge amount of possibilities, including
> > user-defined tuple types.
> 
>    struct TupleWannabe(S) {
>       typeof(S.tupleof) tupleof;
> 
>       static __memberWannabe() {
>          string s;
>          foreach (I, _; typeof(S.tupleof))
>             s ~= `ref `~S.tupleof[I].stringof~`() @property {
>                      return tupleof[`~I.stringof~`];
>                   } `;
>          return s;
>       }
>       mixin(__memberWannabe());
> 
>       alias tupleof this;
>    }
>    
>    struct S {
>       int key;
>       string val;
>    }
> 
>    alias ST = TupleWannabe!S;
> 
>    void main() {
>       ST s = ST(1, "blah");
>       s.key = 2;
>       s.val = "yada";
>       s[0] = 42;
>       s[1] = "etc";
>    }
> 
> SCNR. Happy new year!
[...]

Very nice! Now make it work for int* and string*, but indexing the tuple
returns (non-pointer) int and string. This is the original use case that
prompted this ER, btw, so if it can't be achieved, then we haven't
solved anything.


T

-- 
In theory, there is no difference between theory and practice.


More information about the Digitalmars-d mailing list