New operators opStaticIndex and friends

Simen Kjærås simen.kjaras at gmail.com
Thu May 16 21:36:43 UTC 2019


On Thursday, 16 May 2019 at 21:13:24 UTC, Exil wrote:
> On Thursday, 16 May 2019 at 08:28:35 UTC, Simen Kjærås wrote:
>> struct S {
>>     template opIndex(size_t idx) {
>>         static if (idx == 0) alias opIndex = fun1;
>>         else alias opIndex = fun2;
>>     }
>>
>>     void fun1(size_t i)(Foo!i value) {}
>>     void fun2() {}
>> }
>
> Well now your example doesn't have opIndex(T...)() that your 
> original post did.

Sure it does. There's essentially no difference between fun1 and 
fun2 here:

template fun1(T...) {
     alias fun1 = impl;
}

int impl() {
     return 3;
}

int fun2(T...)() {
     return 3;
}

unittest {
     auto a = fun1!(1,2,3);
     auto b = fun2!(1,2,3);
}

In the same way, whether I write auto opIndex()() {} or alias 
opIndex = ... is irrelevant. The behavior should be exactly the 
same.


> I also don't see how this is a problem specifically if it was 
> named opIndex. Seems more like a detail that would have to be 
> ironed out either way to ensure templates can be used with 
> opStaticIndex/opIndex.

It's a problem because opIndex comes with special syntax that 
other symbols don't have.

--
   Simen


More information about the Digitalmars-d mailing list