getfilter
getfilter creates a function that routes matching calls to one function and all other calls to another.
Syntax
getfilter(filter: FilterBase, original: function, filtered: function) -> function
Parameters
| Parameter | Type | Description |
|---|---|---|
filter | FilterBase | FilterBase value that decides which calls reach the replacement. |
original | function | Function called when the filter does not match. |
filtered | function | Function called when the filter matches. |
Returns
| Type | Description |
|---|---|
function | A wrapper that routes each call to the matching branch. |
Description
getfilter returns a new function that evaluates the filter against its arguments on every call. Matching calls are forwarded to filtered; all other calls are forwarded to original.
Both branches receive the original arguments and may return any number of values. The wrapper preserves yields, so a branch that waits or calls a yielding function resumes normally.
Example
local routed = getfilter(
TypeFilter.new(1, "string"),
function(value) return "original: " .. tostring(value) end,
function(value) return "filtered: " .. value end
)
print(routed("hello")) -- filtered: hello
print(routed(10)) -- original: 10
Notes
- Aliases:
get_filter(filter, original, filtered). - hookfunction accepts a filter as an optional third argument; getfilter accepts one directly.
- The filter is evaluated before either branch runs.