@@ -91,6 +91,50 @@ function RuntimeGeneratedFunction(cache_module::Module, context_module::Module,
9191 opaque_closures = opaque_closures)
9292end
9393
94+ """
95+ @RuntimeGeneratedFunction(function_expression)
96+ @RuntimeGeneratedFunction(context_module, function_expression, opaque_closures=true)
97+
98+ RuntimeGeneratedFunction(cache_module, context_module, function_expression; opaque_closures=true)
99+
100+ Construct a function from `function_expression` which can be called immediately
101+ without world age problems. Somewhat like using `eval(function_expression)` and
102+ then calling the resulting function. The differences are:
103+
104+ * The result can be called immediately (immune to world age errors)
105+ * The result is not a named generic function, and doesn't participate in
106+ generic function dispatch; it's more like a callable method.
107+
108+ You need to use `RuntimeGeneratedFunctions.init(your_module)` a single time at
109+ the top level of `your_module` before any other uses of the macro.
110+
111+ If provided, `context_module` is module in which symbols within
112+ `function_expression` will be looked up. By default this is module in which
113+ `@RuntimeGeneratedFunction` is expanded.
114+
115+ `cache_module` is the module where the expression `code` will be cached. If
116+ `RuntimeGeneratedFunction` is used during precompilation, this must be a module
117+ which is currently being precompiled. Normally this would be set to
118+ `@__MODULE__` using one of the macro constructors.
119+
120+ If `opaque_closures` is `true`, all closures in `function_expression` are
121+ converted to
122+ [opaque closures](https://github.com/JuliaLang/julia/pull/37849#issue-496641229).
123+ This allows for the use of closures and generators inside the generated function,
124+ but may not work in all cases due to slightly different semantics. This feature
125+ requires Julia 1.7.
126+
127+ # Examples
128+ ```
129+ RuntimeGeneratedFunctions.init(@__MODULE__) # Required at module top-level
130+
131+ function foo()
132+ expression = :((x,y)->x+y+1) # May be generated dynamically
133+ f = @RuntimeGeneratedFunction(expression)
134+ f(1,2) # May be called immediately
135+ end
136+ ```
137+ """
94138macro RuntimeGeneratedFunction (code)
95139 quote
96140 RuntimeGeneratedFunction (@__MODULE__ , @__MODULE__ , $ (esc (code)))
@@ -103,9 +147,6 @@ macro RuntimeGeneratedFunction(context_module, code, opaque_closures = true)
103147 end
104148end
105149
106- # Duplicate RuntimeGeneratedFunction docs onto @RuntimeGeneratedFunction
107- @eval @doc $ (@doc RuntimeGeneratedFunction) var"@RuntimeGeneratedFunction"
108-
109150function Base. show (io:: IO , :: MIME"text/plain" ,
110151 f:: RuntimeGeneratedFunction{argnames, cache_tag, context_tag, id} ) where {
111152 argnames,
0 commit comments