I want to create my own Tuple type

Ola Fosheim Grøstad ola.fosheim.grostad at gmail.com
Mon Jan 11 09:42:39 UTC 2021


On Monday, 11 January 2021 at 05:59:03 UTC, Paul Backus wrote:
> 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]`.

Maybe, I would then have to test for release mode and replace 
default: faulting with "unreachable"...

I guess I won't know how this works out without testing, but it 
would in general be nice to be sure that a reinterpret cast to a 
static array would work.

>> 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.

I though maybe it would be nice in general to be able to create 
static indexed type by having a special field name pattern, but I 
will have a another look at staticMap (I don't really want the 
full staticMap into object.d though).

I am a bit worried about compile time implications as I use 
tuples more and more in my own code, and expect that is a common 
trend...

Maybe one could special case arity 0,1,2,3,4,5,6,7,8 and use a 
more generic template for 9 and up.



More information about the Digitalmars-d-learn mailing list