compile-time opIndex

Dicebot via Digitalmars-d digitalmars-d at puremagic.com
Wed Dec 31 12:22:50 PST 2014


On Tuesday, 30 December 2014 at 16:25:05 UTC, Steven 
Schveighoffer wrote:
> On 12/30/14 10:51 AM, Dicebot wrote:
>> On Tuesday, 30 December 2014 at 15:48:03 UTC, Steven 
>> Schveighoffer wrote:
>>> On 12/18/14 11:54 AM, Dicebot wrote:
>>>> I wasn't subscribed to druntime changes thus missed this 
>>>> discussion.
>>>> Will chime in there soon.
>>>
>>> Ping, still waiting on this :)
>>>
>>
>> *blush*
>
> OK, I see your response, but I'm still curious as to 
> how/whether your DIP would solve it. Any thoughts on the 
> feature brought up in this thread?
>
> -Steve

In this specific case I actually expected this to work:

struct S
{
     int key, value;

     import std.typetuple;
     alias asTuple = TypeTuple!(this.tupleof);

     alias asTuple this;
}

void main()
{
     S s;
     s[0] = 42;
}

However it does not seem to recognize that `this` pointer is 
available in such specific invocation. It can be a bug or there 
may be a legitimate reason preventing it - not entirely sure. 
Unfortunately I have never completely understood cases when alias 
is able to preserve context pointer and when it can't.

If it worked though, my proposal would allow more customized 
access:

struct S
{
     int x;

     template MyTuple()
     {
         alias opIndex(size_t i) = S.x;
     }

     alias MyTuple this; // note it is not MyTuple!()
}

void main()
{
     S s;
     s[100] = 42;
     assert(s[50] == 42);
}

It is not exactly the thing you were originally looking for but I 
am against any attempt to try adding template argument list 
flavor to "normal" aggregates - it creates many new corner cases 
to consider. One example was mentioned in DIP - if `static 
opIndex` is the thing why can't you do S[0] but only s[0]? It is 
static after all! More tricky stuff to explain - not worth the 
feature in my opinion.


More information about the Digitalmars-d mailing list