Quirrel4.41.0

types.Class.call

Method on every class
class.call(...): any

Creates 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

...anythe 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

examples/types/class/call.nut
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)
Output:
r.w = 5 r.h = 6

See also

acallCreates 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.
pcallSame as call, except a failure does not invoke the VM's registered error callback.
instanceCreates a new instance of the class without running its constructor.
callCalls the closure, using env as its this and forwarding the rest of the arguments as its own parameters.
classclass index