Thread overview
[Issue 6570] New: 'this' silently passes from one object to another
Aug 29, 2011
Mariusz Gliwiński
Dec 02, 2012
Andrej Mitrovic
Dec 03, 2012
Kenji Hara
August 29, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=6570

           Summary: 'this' silently passes from one object to another
           Product: D
           Version: D2
          Platform: x86_64
        OS/Version: Linux
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: alienballance@gmail.com


--- Comment #0 from Mariusz Gliwiński <alienballance@gmail.com> 2011-08-29 03:42:57 PDT ---
<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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
December 02, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=6570


Andrej Mitrovic <andrej.mitrovich@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |andrej.mitrovich@gmail.com
         Resolution|                            |INVALID


--- Comment #1 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2012-12-02 10:28:46 PST ---
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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
December 03, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=6570



--- Comment #2 from Kenji Hara <k.hara.pg@gmail.com> 2012-12-02 18:57:36 PST ---
(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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------