Bug 8097 – opDispatch produces confusing error message when rejected by a template constraint
Status
RESOLVED
Resolution
DUPLICATE
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2012-05-15T07:14:00Z
Last change time
2013-11-16T21:08:52Z
Keywords
diagnostic
Assigned to
nobody
Creator
turkeyman
Comments
Comment #0 by turkeyman — 2012-05-15T07:14:57Z
I have a case like this:
template isValid(string s)
{
// screen for valid opDispatch patterns
enum bool isValid = s == "foo";
}
struct T
{
int opDispatch(string s) if(isValid!s)
{
// do something
return 0;
}
int a;
}
T t;
auto i = t.a; <-- this is fine
auto j = t.foo; <-- this is captured by opDispatch, this is fine.
auto k = t.bar; <-- opDispatch is not invoked with "bar", so this fails, but produces a horrible error message
If opDispatch is not present, an attempt to access bar produces the error:
Error: No property 'bar' for type T
If opDispatch is present, even though the constraint prevents it from being invoked with the given argument, it produces a very confusing template error:
Error: template instance opDispatch!("bar") opDispatch!("bar") does not match template declaration opDispatch(string s) if (isValid!s)
This has confused everyone in the building so far (new to D). People are not expecting a template error when they make a typo, or address something that isn't a member of the class.
It would be nice if when opDispatch has constraints on it, preventing the template from being invoked under some conditions, that it would revert to the original expected error message.
If the compiler would say "okay, bar didn't match and neither did opDispatch invoke with it, give a normal error, not a template instantiation error", this would save some confusion :)
Comment #1 by turkeyman — 2013-09-29T22:16:31Z
This seems to be fixed, but a new issue has taken it's place; if there is an
error in the opDispatch, or even in it's constraint, it will silently fail and
produce the error "No property: blah", as if there were no opDispatch at all.
Ie, it no longer gives compile errors within opDispatch, and compile errors are
useful :)
Comment #2 by yebblies — 2013-11-16T21:08:52Z
*** This issue has been marked as a duplicate of issue 8387 ***