struct S { template opDispatch (string name) { void opCall (T) () @property {} } } struct S3 { auto opCall (T) () @property { struct S4 { void opDispatch (string name) () @property {} } S4 s4; return s4; } } void main () { S s; s.opDispatch!"x".opCall!int; // OK s.x.opCall!int; // OK s.opDispatch!("x")!(int); // parse error s.x!int; // infinite loop in dmd S3 s3; s3.opCall!int.opDispatch!("x"); // OK s3.opCall!int.x; // OK s3!int.x; // Error: template instance s3!(int) s3 is not a template declaration, it is a variable }
The infinite loop in dmd is the real issue. struct S { template opDispatch (string name) { //void opCall (T) () @property {} // not need } } void main () { S s; s.x!int; // infinite loop in dmd } And others are not errors, because their error messages are valid. Then I replace the subject. ---- opCall works only for call syntax, e.g. rewriting foo(args) to foo.opCall(args). If you want to implement s.x!int with opDispatch, *nested opDispatch idiom* like follows would work. struct S { template opDispatch(string name) { T opDispatch(T)() @property { return T.init; } } } void main() { S s; assert(s.x!int == 0); // same as s.opDispatch!("x").opDispatch!(int)() }
https://github.com/D-Programming-Language/dmd/pull/808
Commit pushed to master at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/380b2f8a26da0cb5c6a263608efb2c7020f25c7e Merge pull request #808 from 9rnsr/fix7702 Issue 7702 & 5733 - opDispatch goes into infinite loop