Bug 4989 – opDispatch not used when alias this is present
Status
RESOLVED
Resolution
DUPLICATE
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
Other
OS
Linux
Creation time
2010-10-04T10:38:00Z
Last change time
2010-11-30T05:25:15Z
Assigned to
nobody
Creator
schveiguy
Comments
Comment #0 by schveiguy — 2010-10-04T10:38:27Z
Example:
import std.stdio;
struct X
{
int foo() { return 1; }
bool foo2() {return true;}
}
struct S
{
X x;
// override only int functions
auto opDispatch(string fn, Args...)(Args args) if (is(typeof(mixin("x." ~ fn ~ "(args)")) == int))
{
writeln("calling " ~ fn);
mixin("return x." ~ fn ~ "(args);");
}
// override functions that aren't supported by X
void opDispatch(string fn, Args...)(Args args) if (!is(typeof(mixin("x." ~ fn ~ "(args)"))))
{
writeln("invalid function " ~ fn);
}
// let all others pass through (i.e. foo2)
alias x this;
}
void main()
{
S s;
s.foo();
s.foo2();
// s.baz(); // compiler error "Error: no property 'baz' for type 'X'"
}
when compiled, this outputs nothing. I'd expect to see (if last line of main is uncommented):
calling foo
invalid function baz
Note the glaring use case here is being able to have an implicit cast, yet override the behavior of the implicitly casted member.
At the very least, opDispatch should be used when the alias this'd value does not support the function.
IMO, opDispatch should be preferred over the alias this'd member. Simply because it's possible (though ugly) to select which opDispatch functions compile, but it's impossible to selectively pick members to compile via alias this.
Comment #1 by simen.kjaras — 2010-11-30T03:19:43Z
*** Issue 4224 has been marked as a duplicate of this issue. ***
Comment #2 by schveiguy — 2010-11-30T05:25:15Z
*** This issue has been marked as a duplicate of issue 4224 ***