I have a struct that I am using like a Tuple, but I want to be able to opIndex in a different way than Tuple's opIndex. I want to be able to opIndex whatever is underlying the Tuple.

The code below works, but is kind of annoying because to extend you have to keep adding static ifs. I want to change it to a recursion that can handle any length. I tried a few different options, but not having much luck.

Would appreciate any advice!

Note: I left out the function foo, but think of foo is to Foo as tuple is to Tuple.

import std.typecons : Tuple;

struct Foo(T...)
{
        alias U = Tuple!T;
        U underlying;
        alias underlying this;
        
        alias Names = U.fieldNames;
        alias Types = U.Types;
        
        template process(B...)
        {
                auto ref process(A...)(A a)
                {
                        alias fooB = foo!(B);
                        
                        static if (A.length == 1)
                        {
                                return fooB(this.underlying[0][a[0]]);
                        }
                        else static if (A.length == 2)
                        {
                                return fooB(this.underlying[0][a[0]],
                                            this.underlying[1][a[1]]);
                        }
                        else static if (A.length == 3)
                        {
                                return fooB(this.underlying[0][a[0]],
                                            this.underlying[1][a[1]],
                                            this.underlying[2][a[2]]);
                        }
                }
        }
        
        auto ref opIndex(Slices...)(Slices slices)
                if (Slices.length == Types.length)
        {
                return process!(Names)(slices);
        }
}

Reply via email to