Quirrel4.41.0

types.Function.call

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

Calls the closure, using env as its this and forwarding the rest of the arguments as its own parameters.

Parameters

...anythe first argument becomes this inside the call; the rest are forwarded as the closure's own arguments repeats

Return value

Whatever the call returns.

Errors

Whatever the call itself throws, propagated unchanged.

Notes

env is not optional: f.call() fails with the arity error of f itself (zero arguments reached it, including the missing this), not an error from call. Anything beyond env is optional only in the sense that f may default or ignore it - call forwards what it is given.

When f is a non-native, non-generator closure, call tail-calls into it: the frame for this call does not stay on the stack, so f.call(env, ...) in a function's own tail position recurses as cheaply as a direct call. pcall never does this. Calling a generator-producing function through call behaves like calling it directly: it returns a new suspended generator without running the body.

Example

examples/types/function/call.nut
function greet(prefix) { return $"{prefix} {this.name}" }
let bob = {name = "Bob"}
println("greet.call(bob, \"Hi\") =", greet.call(bob, "Hi"))

// calling a generator-producing function through call() just creates the
// generator, exactly like calling it directly
function geny(n) { yield n }
let g = geny.call({}, 3)
println("typeof g + \" \" + resume g =", typeof g, resume g)
Output:
greet.call(bob, "Hi") = Hi Bob
typeof g + " " + resume g = generator 3

See also

pcallCalls the closure like call does, using env as its this and forwarding the rest of the arguments as its own parameters.
acallCalls the closure with the arguments taken from an array: args[0] becomes this, and the rest of args becomes the closure's own parameters.
bindenvReturns a copy of the closure with env statically bound as its this: every future call to the copy uses env, no matter how it is called.
functionclass index