Public API
RuntimeGeneratedFunctions.RuntimeGeneratedFunction — Type
RuntimeGeneratedFunction(cache_module, context_module, function_expression;
opaque_closures = true)A callable function compiled from function_expression without a world-age barrier. A RuntimeGeneratedFunction is a Function, so generic code that accepts and invokes a Function can use it. It is not a named generic function: it represents one callable method and does not participate in method dispatch.
Arguments
cache_module::Module: module that owns the cached expression. It must have been initialized withinit. During precompilation, pass the module currently being precompiled so the cache survives precompilation.context_module::Module: module in which global names infunction_expressionare resolved. It must have been initialized withinit.function_expression::Expr: an anonymous-function or function-definition expression whose body becomes the callable function.
Keywords
opaque_closures = true: convert closures and generators infunction_expressionto Julia opaque closures. This permits closures in the generated body, with Julia's opaque-closure semantics.
Fields
body: cached body expression while it is retained, ornothingafterdrop_expr. The function remains callable after dropping its local expression reference because the cache owns the body.
Serialization
Serialization.serialize preserves a runtime-generated function, including after drop_expr. A dropped function's expression is recovered from its cache while serializing and remains dropped after deserialization.
Examples
RuntimeGeneratedFunctions.init(@__MODULE__)
square = RuntimeGeneratedFunction(@__MODULE__, @__MODULE__, :(x -> x^2))
square(3)RuntimeGeneratedFunctions.@RuntimeGeneratedFunction — Macro
@RuntimeGeneratedFunction(function_expression)
@RuntimeGeneratedFunction(context_module, function_expression,
opaque_closures = true)Construct a RuntimeGeneratedFunction from a function expression using the calling module as its cache module. Call init at the top level of that module before expanding this macro.
Arguments
function_expression::Expr: an anonymous-function or function-definition expression to compile.context_module::Module: optional module in which global names infunction_expressionare resolved. By default, names resolve in the calling module.
Keywords
opaque_closures = true: convert closures and generators in the expression to Julia opaque closures.
Examples
RuntimeGeneratedFunctions.init(@__MODULE__)
increment = @RuntimeGeneratedFunction(:(x -> x + 1))
increment(2)RuntimeGeneratedFunctions.drop_expr — Function
drop_expr(rgf::RuntimeGeneratedFunction)Return a copy of rgf that does not retain its function body expression. This allows the expression AST to be garbage collected while keeping the function callable.
Arguments
rgf::RuntimeGeneratedFunction: generated function whose retained expression should be released.
Returns
- A
RuntimeGeneratedFunctionwithbody === nothing. It has the same callable behavior asrgfwhile its cache entry remains available.
The expression can still be retrieved later using get_expression as long as at least one RuntimeGeneratedFunction with the same body exists.
Examples
ex = :((x) -> x^2)
rgf = @RuntimeGeneratedFunction(ex)
rgf_dropped = drop_expr(rgf)
rgf_dropped(2) # Still works, returns 4Setup and Inspection API
RuntimeGeneratedFunctions.init — Function
RuntimeGeneratedFunctions.init(mod)Initialize mod for runtime-generated functions.
Arguments
mod::Module: module that will cache generated expression bodies and own the generated call method.
Rules
Call init once at module top level before using @RuntimeGeneratedFunction in mod, or before passing mod as either the cache or context module to RuntimeGeneratedFunction. Repeated calls are idempotent. Do not call it from a local scope: it defines a generated method in mod so that global names resolve in that module.
Examples
module GeneratedFunctionsExample
using RuntimeGeneratedFunctions
RuntimeGeneratedFunctions.init(@__MODULE__)
endRuntimeGeneratedFunctions.get_expression — Function
get_expression(rgf::RuntimeGeneratedFunction)Retrieve the function expression from a RuntimeGeneratedFunction.
Arguments
rgf::RuntimeGeneratedFunction: generated function whose source expression is requested.
Returns
- An anonymous-function
Exprcontaining the normalized argument names and the cached body.
This works even if drop_expr has been called on the function, as long as the expression is still in the cache (i.e., at least one RuntimeGeneratedFunction with the same body exists).
Examples
ex = :((x) -> x^2)
rgf = @RuntimeGeneratedFunction(ex)
RuntimeGeneratedFunctions.get_expression(rgf)
# Returns: :((x,) -> x ^ 2)