A opIndexUnary quiz

bearophile bearophileHUGS at lycos.com
Tue Jan 1 19:52:19 PST 2013


Era Scarecrow:

>  Well I see that you have opIndexUnary twice; According to the 
> manual you wouldn't need as it would rewrite the code so you 
> only need it once;

And as you have seen if you remove the useles opIndexRight the 
program keeps compiling with no errors and keeps asserting at 
run-time:



struct Foo {
     int x;
     alias this = x;
}

class Bar {
     Foo[] data;

     this() {
         data.length = 10;
     }

     Foo opIndex(uint i) {
         return data[i];
     }

     void opIndexUnary(string op)(uint i) if (op == "++") {
         data[i]++;
     }
}

void main() {
     auto barfoo = new Bar;
     ++barfoo[3];
     assert(barfoo.data[3] == 1);
     barfoo[3]++;
     assert(barfoo.data[3] == 2);
}


Bye,
bearophile


More information about the Digitalmars-d-learn mailing list