ecmascript-types
ecmascript-types copied to clipboard
Discussion: Function / Class overloading
This is touched on very briefly in the spec, but how would one deal with a named function or class method having 2 or more overloaded implementations in terms of discovery.
For example:
(function() {
var Point2D_addXY;
class Point2D {
constructor(x:number, y:number) {
this._x = x;
this._y = y;
}
constructor(x:number) {
this._x = x;
this._y = 0;
}
addXY() {
if (this && (this instanceof Point2D) && this.addXY === Point2D_addXY) {
return this._x + this._y;
}
}
addXY(moreX:number) {
if (this && (this instanceof Point2D) && this.addXY === Point2D_addXY) {
return this._x + moreX + this._y;
}
}
addXY(moreX:number, moreY:number) {
if (this && (this instanceof Point2D) && this.addXY === Point2D_addXY) {
return this._x + moreX + this._y + moreY;
}
}
}
Point2D_addXY = Point2D.prototype.addXY; /* which one is it? */
return Point2D
})();
While a bit unorthodox, the addXY methods above are ensuring that the given object that is being used as this this pointer has not some how overwritten the method. . .
But the point is what would Point2D_addXY
s value be. . . how do you address this problem?
My first thought is that you could overload class methods, and that they just get a wrapper around them by the engine. . .
I suppose you could do the same thing (a wrapper) for functions that are written multiple times with different arg signatures
But in either case how would you change / swap one of those. . . wrapper dosesn't solve that. ..
Discuss. . .