Polymorphic "this" for static members
When trying to implement a fairly basic, but polymorphic, active record style model system we run into issues with the type system not respecting `this` when used in conjunction with a constructor or template/generic. I've posted before about this here, #5493, and #5492 appears to mention this behavior also. And here is an SO post this that I made: http://stackoverflow.com/questions/33443793/create-a-generic-factory-in-typescript-unsolved I have recycled my example from #5493 into this ticket for further discussion. I wanted an open ticket representing the desire for such a thing and for discussion but the other two are closed. Here is an example that outlines a model `Factory` which produces models. If you want to customize the `BaseModel` that comes back from the `Factory` you should be able to override it. However this fails because `this` cannot be used in a static member. ``` typescript // Typically in a library export interface InstanceConstructor<T extends BaseModel> { new(fac: Factory<T>): T; } export class Factory<T extends BaseModel> { constructor(private cls: InstanceConstructor<T>) {} get() { return new this.cls(this); } } export class BaseModel { // NOTE: Does not work.... constructor(private fac: Factory<this>) {} refresh() { // get returns a new instance, but it should be of // type Model, not BaseModel. return this.fac.get(); } } // Application Code export class Model extends BaseModel {
Posted by tlong-dev on Dec 1, 2015, 8:23 PM
Tags
Related issues
- struct's lifetime requirements can be bypassed via covariant field assignment
- Proposal to Reorganize the Moderation Team and Review Moderation Policies
- Progressive FPS degradation appears to be related to dota2.com — blocking the domain prevents the performance loss
- proposal: container/...: generic collection types
- Better Custom Endpoint BYOK
- A concrete policy for backward compatibility until Godot 5.0
Raise real-world problems, find people facing the same issue, and discuss solutions.5,000 issues0 comments
github (Other)
struct's lifetime requirements can be bypassed via covariant field assignment
Proposal to Reorganize the Moderation Team and Review Moderation Policies
Progressive FPS degradation appears to be related to dota2.com — blocking the domain prevents the performance loss
proposal: container/...: generic collection types
Better Custom Endpoint BYOK
A concrete policy for backward compatibility until Godot 5.0
New MTP models not supported
Tracking: 429 / Capacity Issues
Polymorphic "this" for static members
When trying to implement a fairly basic, but polymorphic, active record style model system we run into issues with the type system not respecting `this` when used in conjunction with a constructor or template/generic. I've posted before about this here, #5493, and #5492 appears to mention this behavior also. And here is an SO post this that I made: http://stackoverflow.com/questions/33443793/create-a-generic-factory-in-typescript-unsolved I have recycled my example from #5493 into this ticket for further discussion. I wanted an open ticket representing the desire for such a thing and for discussion but the other two are closed. Here is an example that outlines a model `Factory` which produces models. If you want to customize the `BaseModel` that comes back from the `Factory` you should be able to override it. However this fails because `this` cannot be used in a static member. ``` typescript // Typically in a library export interface InstanceConstructor<T extends BaseModel> { new(fac: Factory<T>): T; } export class Factory<T extends BaseModel> { constructor(private cls: InstanceConstructor<T>) {} get() { return new this.cls(this); } } export class BaseModel { // NOTE: Does not work.... constructor(private fac: Factory<this>) {} refresh() { // get returns a new instance, but it should be of // type Model, not BaseModel. return this.fac.get(); } } // Application Code export class Model extends BaseModel {
Comments (0)
No comments yet. Start the discussion.