types.Class.call
classCreates a new instance of the class and runs its constructor, the same as writing ClassName(...). Exists so a class value obtained indirectly (through a variable, a table slot, or getclass()) can still be instantiated through a uniform .call(...) spelling shared with functions.
Parameters
... | any | the constructor's arguments, with one extra leading value that is required but ignored (see Notes) repeats |
Return value
The new instance.
Errors
Whatever the constructor throws propagates uncaught. Passing fewer stack slots than the constructor needs (counting the ignored leading one) throws wrong number of parameters passed to native closure 'constructor' (N passed, M required), naming the constructor, not call.
Notes
function.call(_this, args...) takes an explicit this as its first argument, because a plain function's this is otherwise whatever the caller bound. call is registered identically for classes, so the same leading slot is required here too - but a class always builds its own instance to serve as this, and silently overwrites whatever was passed in that slot with it. So SomeClass.call(a, b) does not call the constructor with (a, b): it calls the constructor with (b) alone, having thrown a away. Pass null as the first argument to make that visible in the calling code, and see acall for the same rule in array form.
Example
class Rect {
w = 0
h = 0
constructor(w, h) { this.w = w; this.h = h }
}
// the leading argument is a required placeholder, discarded in favor of the new instance
let r = Rect.call(null, 5, 6)
println("r.w =", r.w, "r.h =", r.h)r.w = 5 r.h = 6See also
acall | Creates a new instance of the class, passing it the constructor arguments packed into an array. Equivalent to call, only with the arguments collected into one array instead of listed one by one. |
pcall | Same as call, except a failure does not invoke the VM's registered error callback. |
instance | Creates a new instance of the class without running its constructor. |
call | Calls the closure, using env as its this and forwarding the rest of the arguments as its own parameters. |
| class | class index |