Quirrel4.41.0

types.Function.weakref

Method on every function
weakref(): any

Returns a weak reference to the closure.

Return value

A weakref value. Calling its ref() method gives the closure back for as long as something else still holds a strong reference to it; once the closure's refcount drops to zero, ref() returns null instead.

Notes

Takes no arguments. This is a weak reference to the closure value itself - unrelated to the weak reference bindenv keeps on the environment it binds inside a closure.

Example

examples/types/function/weakref.nut
local f = function() {}
let w = f.weakref()
println("typeof w, (w.ref() == f) =", typeof w, w.ref() == f)
f = null
println("w.ref() =", w.ref())
Output:
typeof w, (w.ref() == f) = weakref true
w.ref() = null

See also

weakrefReturns a weak reference to the table.
cloneReturns the closure itself: unlike a table, array or class instance, a closure has no separate clone identity.
functionclass index