Bug 6570 – 'this' silently passes from one object to another
Status
RESOLVED
Resolution
INVALID
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2011-08-29T03:42:00Z
Last change time
2012-12-02T18:57:36Z
Assigned to
nobody
Creator
alienballance
Comments
Comment #0 by alienballance — 2011-08-29T03:42:57Z
<code>
import std.stdio;
class T {
abstract string type() {
return "T";
}
}
class T2 : T {
override string type() {
return "T2";
}
void test(T)() {
writeln(T.type);
}
}
void main() {
(new T2).test!T();
}
</code>
<output>T2</output>
type() isn't static method so AFAIK it shouldn't be possible to call it from test(). Instead of that, call is possible, and local 'this' is used.
Comment #1 by andrej.mitrovich — 2012-12-02T10:28:46Z
I don't know why 'T2' was in output, it should be 'T' (and it is now).
As for why it works: this is used to pick a specific override in some base class, e.g.:
class A
{
void test() { }
}
class B : A
{
override void test() { }
}
class C : B
{
override void test() { A.test(); }
}
If you use 'super.test()' you would end up calling B.test instead of A.test.
Comment #2 by k.hara.pg — 2012-12-02T18:57:36Z
(In reply to comment #1)
> I don't know why 'T2' was in output, it should be 'T' (and it is now).
>
> As for why it works: this is used to pick a specific override in some base
> class, e.g.:
>
> class A
> {
> void test() { }
> }
>
> class B : A
> {
> override void test() { }
> }
>
> class C : B
> {
> override void test() { A.test(); }
> }
>
> If you use 'super.test()' you would end up calling B.test instead of A.test.
That is bug 8809 and was fixed in 2.061head.