This repository has been archived by the owner on Jul 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: support extension through abstract base class (#18)
This is a proposal based on [this comment](https://github.com/monadahq/polycons/pull/15/files#r933560022). The basic idea is that both the polycon class (e.g. `Dog`) and the concrete class (`Poodle`) extend a common abstract base type (e.g. `DogBase`). This has the following effects: 1. No need to protect against stack overflow (the concrete class doesn't actually extend the polycon). 2. It is now possible to instantiate any concrete type directly (added a test that instantiates a `Labrador` even if when `Dog` resolves to `Poodle`. 3. We can still share common implementation at the base. 4. Enforce that concrete classes actually implement all abstract methods and properties (previously this was only by convention). The polycon constructor has a quirky pattern: ```ts constructor(scope, id, props) { super(null as any, id, props); return Polycons.create(QUALIFIER, scope, id, props); } ``` The call to `super()` with `null` as the `scope` does two things: 1. The base construct is not attached to our tree. 2. The `null` can be used by the base class to bail out early: ```ts class DogBase { public readonly foo: number; constructor(scope, id, props) { super(scope, id); if (!scope) { // initialize all readonly props with dummy values, this obj is thrown away this.foo = -1; return; } this.foo = 7947; // <-- the real thing } } ```
- Loading branch information
Showing
5 changed files
with
154 additions
and
169 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,2 @@ | ||
export * from "./process"; | ||
export * from "./polycon"; | ||
export * from "./polycon-factory"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.