Closures

hookfunction

Replaces the behavior of a target function with a replacement function and returns a callable copy of the original behavior.

Syntax

hookfunction(target: function, replacement: function): function
Aliases hookfuncreplaceclosuredetourfunctiondetour_function

Arguments

NameTypeDescription
targetfunctionFunction to patch. Passing a non-function raises a type error.
replacementfunctionFunction whose function is installed into or mapped onto target. Passing a non-function raises a type error.

Returns

NameTypeDescription
originalfunctionCallable copy of the original target behavior before the hook was installed.

Description

Replaces the behavior of a target function with a replacement function and returns a callable copy of the original behavior. Both arguments must be functions.

Call it with 2 parameter(s): target, replacement. The argument table explains which values are required and which ones only refine the behavior.

It returns original (function). Use the returns table to separate successful values from nil results and recoverable errors.

Example

Patch a function closure and keep a cloned original.

local function target(value)
    return value + 1
end

local original
original = hookfunction(target, function(value)
    return original(value) * 2
end)