Comment #0 by michal.minich — 2012-03-13T06:29:49Z
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
}
Comment #1 by k.hara.pg — 2012-03-13T07:30:50Z
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)()
}