fasttime / Polytype

Dynamic multiple inheritance for JavaScript and TypeScript. Without mixins.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Method override does not work correctly with "crocks" library

creatorrr opened this issue · comments

Steps to reproduce:

const Arrow = require("crocks/Arrow");

class K extends classes(Arrow, Object) {
  compose(...args) {
    console.log("THIS WILL NOT RUN");
    return super.compose(...args);
  }
}

const k = new K([x => x]);
const l = new K([x => x]);
k.compose(l);  // returns an instance of Arrow but no console.log

This thing is that crocks defines a new compose method on each instance of Arrow, rather than defining a single compose method for all Arrows.
This is not an issue in Polytype, it's intended that you can define a method on a specific object and that would take precedence over methods defined in a base class.

To get what you want you could probably hook yourself in the constructor and overwrite the compose method with your own version.

class K extends classes(Arrow, Object) {
  constructor (arrowArgs) {
    super(arrowArgs);
    const originalCompose = this.compose;
    this.compose = function (...args) {
      console.log("THIS WILL NOT RUN");
      return originalCompose.apply(this, args);
    }
  }
}

Note that there is no point in calling super.compose in your class because there is no compose method in any superclass, it's all on the current object.

Ah, that makes sense. Yes, that is the workaround I am currently using. I love crocks but I am guessing that to fix this, one would have to rewrite most of the constructor functions. Anyway, thank you @fasttime ; closing this issue for now.