2
votes
ref auto opIndex(size_t i){
   return t[i];
}

Here t is a tuple and i needs to be read at compile time. How would I express this in D?

1
I don't think you can have a compile-time opIndex. - Colonel Thirty Two
did you try adding constness inout ref auto opIndex(size_t i) inout - ratchet freak

1 Answers

2
votes

There isn't any clean way to do this with opIndex currently, for two reasons. First is simple - it isn't implemented. That would be relatively easy to fix on its own but there is a second reason - it adds serious context sensitivity to language grammar.

Consider this struct definition:

struct S
{
    // imagine this works, syntax is not important
    static int opIndex (size_t i) { return 42; }
}

Now what does the code S[10] mean? Is it a static array type of ten S elements? Or static opIndex call which returns 42? It is impossible to tell without knowing quite a lot of context and in certain cases impossible to tell at all (like typeof(S[10])).

Somewhat relevant (unapproved!) idea: http://wiki.dlang.org/DIP63