Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inconsistency in polymorphic 'this' typing #5492

Closed
zhuravlikjb opened this issue Nov 2, 2015 · 3 comments
Closed

Inconsistency in polymorphic 'this' typing #5492

zhuravlikjb opened this issue Nov 2, 2015 · 3 comments
Labels
By Design Deprecated - use "Working as Intended" or "Design Limitation" instead

Comments

@zhuravlikjb
Copy link

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'
@zhuravlikjb
Copy link
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"?

@mhegazy mhegazy added the By Design Deprecated - use "Working as Intended" or "Design Limitation" instead label Nov 2, 2015
@mhegazy
Copy link
Contributor

mhegazy commented Nov 2, 2015

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.

@mhegazy mhegazy closed this as completed Nov 2, 2015
@zhuravlikjb
Copy link
Author

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

@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
By Design Deprecated - use "Working as Intended" or "Design Limitation" instead
Projects
None yet
Development

No branches or pull requests

2 participants