On my free time I have been tinkering with Nim, and have taken a liking to the
idea of building my personal website (almost) entirely with Nim. There are some
js libs I frequently use, so I took this opportunity to try and create some Nim
bindings. Mostly, the experience has been pretty smooth and awesome! I do have
some questions though (I am far from a Nim expert, mind you).
Sometimes, js libs will have functions defined on the prototype of an object,
and these will behave differently depending on the arguments passed. Say for
example:
global.Vector.prototype.add = function(x, y, z) {
if (x instanceof global.Vector) {
this.x -= x.x || 0;
this.y -= x.y || 0;
this.z -= x.z || 0;
return this;
}
if (x instanceof Array) {
this.x -= x[0] || 0;
this.y -= x[1] || 0;
this.z -= x[2] || 0;
return this;
}
this.x -= x || 0;
this.y -= y || 0;
this.z -= z || 0;
return this;
}
As far as I can tell, such a function must be a property of the type.
type
VectorObj{.importc.} = object
add*: proc(...)
But it can't be redefined there and varargs and OR-ing (ie. proc(x: float |
Vector | openArray[float], y, z: float = 0) are not concrete.
Is there some established practice on how to wrap such a function?