I want to create my own Tuple type

Paul Backus snarwin at gmail.com
Mon Jan 11 05:59:03 UTC 2021


On Monday, 11 January 2021 at 05:44:19 UTC, Ola Fosheim Grostad 
wrote:
> On Monday, 11 January 2021 at 01:49:26 UTC, Paul Backus wrote:
>> Why are these particular implementation details important to 
>> you?
>
> It is for object.d.
>
> I want to allow fast runtime indexing if all elements are of 
> the same type.

static if (allSameType) {
     auto opIndex(size_t i) {
         switch (i) {
             static foreach (j; 0 .. Types.length) {
                 case j: return this.expand[j];
             }
             default: assert(0); // or throw RangeError
         }
     }
}

Any decent compiler will turn this into `return this.expand[i]`.

> If the types are different I want static indexing, so the plan 
> is to resolve failed lookup as __0 etc by modifying the 
> compiler.

You can just fall back to `alias expand this` like Phobos's Tuple 
does in this case. No compiler modification needed.


More information about the Digitalmars-d-learn mailing list