Skip to content
This repository has been archived by the owner on Jul 16, 2023. It is now read-only.

chore: support extension through abstract base class #18

Merged
merged 8 commits into from
Jul 31, 2022

Conversation

eladb
Copy link
Contributor

@eladb eladb commented Jul 29, 2022

This is a proposal based on this comment.

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:

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:
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
  }
}

src/polycons.ts Outdated Show resolved Hide resolved
test/polycon.test.ts Outdated Show resolved Hide resolved
test/polycon.test.ts Outdated Show resolved Hide resolved
@eladb eladb marked this pull request as ready for review July 29, 2022 20:21
@eladb eladb requested a review from Chriscbr July 29, 2022 20:21
@mergify mergify bot merged commit 769f934 into rybickic/base Jul 31, 2022
@mergify mergify bot deleted the eladb/base-pattern branch July 31, 2022 17:04
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants