Skip to content

Inconsistency in polymorphic 'this' typing #5492

Closed

Description

class A {
    p: A;
    c = this.p;
}

class B extends A {
    p: B;    
}

var s: B;
var k = s.c; // 'k' is 'A'
class A {
    c = this;
}

class B extends A {        
}

var s: B;
var k = s.c; // 'k' is 'B'

Activity

  1. zhuravlikjb commented on Nov 2, 2015

    @zhuravlikjb
    Author

    After some experiments, a simple question: is 'this' polymorphic only for simple expression "this", and not when it's used as a qualifier in some reference expression, e.g. "this.foo"?

  2. mhegazy commented on Nov 2, 2015

    @mhegazy
    Contributor

    One thing to note, there are two uses of the keyword this. your example above uses an initializer. this will be evaluated once, in the context of the base class A; the type of the expression this.p here is A, and there is no polymorphism here. if you however, gave c the type this, this will be instantiated in the context of the derived class, and you would get the desired polymorphism.

  3. zhuravlikjb commented on Nov 3, 2015

    @zhuravlikjb
    Author

    Thanks, now it's more clear how it behaves.

  4. locked and limited conversation to collaborators on Jun 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    By DesignDeprecated - use "Working as Intended" or "Design Limitation" instead

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions