From time to time we need to interop with libraries that have JavaScript classes that we need to extend. I think this trend will grow as ES2015 and TypeScript become more widespread.
Currently, I don’t have a better way to do this interop than extending the class in a [%%bs.raw ...]
block and binding to the subclass with BuckleScript bindings immediately below that. E.g., https://stackoverflow.com/q/46454098/20371 .
However, since ES2015 inheritance is a syntax sugar for prototypal inheritance and is likely to stay that way, it should be possible to write inheriting classes by binding to the right sequence of calls for prototypal inheritance. For example, if I have a class:
class Animal {
speak() {}
}
We should be able to get BuckleScript to output something like this:
var Dog = Object.create(Animal);
Dog.prototype.constructor = function(name) {
this.name = name;
};
Dog.prototype.speak = function() {
console.log(this.name + " says woof!!");
};
Ideally, usage would look something like this:
module Dog = Class.Extend({
type t;
let supertype = "Animal";
let make(name) = Class.Instance.make({
"name": name
});
let speak(t) = Class.Instance.method(instance =>
Js.log2(instance##name, "says woof!!");
);
});
I’m probably missing a lot of corner cases here does anybody have anything else they’re doing to extend classes?