Skip to content

Commit 51adcc8

Browse files
Documentation improvements: fix typo and add docstrings
- Fix typo "fixable" -> "flexible" in docs/src/index.md - Fix "Retrieving Expressions" example (code was shown in wrong order) - Add docstring for `drop_expr` function - Add docstring for `get_expression` function 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 2145c42 commit 51adcc8

File tree

3 files changed

+40
-3
lines changed

3 files changed

+40
-3
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,10 @@ From a constructed RuntimeGeneratedFunction, you can retrieve the expressions us
117117
`RuntimeGeneratedFunctions.get_expression` command. For example:
118118

119119
```julia
120-
julia> RuntimeGeneratedFunctions.get_expression(rgf)
121120
ex = :((x) -> x^2)
122121
rgf = @RuntimeGeneratedFunction(ex)
122+
julia> RuntimeGeneratedFunctions.get_expression(rgf)
123+
:((x,) -> x ^ 2)
123124
```
124125

125126
This can be used to get the expression even if `drop_expr` has been performed.

docs/src/index.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ builds functions in a way that avoids `eval`.
66

77
Note that `RuntimeGeneratedFunction` does not handle closures. Please use the
88
[GeneralizedGenerated.jl](https://github.com/JuliaStaging/GeneralizedGenerated.jl)
9-
package for more fixable staged programming. While `GeneralizedGenerated.jl` is
9+
package for more flexible staged programming. While `GeneralizedGenerated.jl` is
1010
more powerful, `RuntimeGeneratedFunctions.jl` handles large expressions better.
1111

1212
## Tutorials and Documentation
@@ -108,9 +108,10 @@ From a constructed RuntimeGeneratedFunction, you can retrieve the expressions us
108108
`RuntimeGeneratedFunctions.get_expression` command. For example:
109109

110110
```julia
111-
julia> RuntimeGeneratedFunctions.get_expression(rgf)
112111
ex = :((x) -> x^2)
113112
rgf = @RuntimeGeneratedFunction(ex)
113+
julia> RuntimeGeneratedFunctions.get_expression(rgf)
114+
:((x,) -> x ^ 2)
114115
```
115116

116117
This can be used to get the expression even if `drop_expr` has been performed.

src/RuntimeGeneratedFunctions.jl

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,24 @@ struct RuntimeGeneratedFunction{argnames, cache_tag, context_tag, id, B} <: Func
8181
end
8282
end
8383

84+
"""
85+
drop_expr(rgf::RuntimeGeneratedFunction)
86+
87+
Return a new `RuntimeGeneratedFunction` that does not hold a reference to the
88+
function body expression. This allows the expression AST to be garbage collected
89+
while keeping the function callable.
90+
91+
The expression can still be retrieved later using [`get_expression`](@ref) as long
92+
as at least one `RuntimeGeneratedFunction` with the same body exists.
93+
94+
# Examples
95+
```julia
96+
ex = :((x) -> x^2)
97+
rgf = @RuntimeGeneratedFunction(ex)
98+
rgf_dropped = drop_expr(rgf)
99+
rgf_dropped(2) # Still works, returns 4
100+
```
101+
"""
84102
function drop_expr(::RuntimeGeneratedFunction{
85103
a,
86104
cache_tag,
@@ -334,6 +352,23 @@ function closures_to_opaque(ex::Expr, return_type = nothing)
334352
return Expr(head, Any[closures_to_opaque(x, return_type) for x in args]...)
335353
end
336354

355+
"""
356+
get_expression(rgf::RuntimeGeneratedFunction)
357+
358+
Retrieve the function expression from a `RuntimeGeneratedFunction`.
359+
360+
This works even if [`drop_expr`](@ref) has been called on the function, as long as
361+
the expression is still in the cache (i.e., at least one `RuntimeGeneratedFunction`
362+
with the same body exists).
363+
364+
# Examples
365+
```julia
366+
ex = :((x) -> x^2)
367+
rgf = @RuntimeGeneratedFunction(ex)
368+
RuntimeGeneratedFunctions.get_expression(rgf)
369+
# Returns: :((x,) -> x ^ 2)
370+
```
371+
"""
337372
function get_expression(rgf::RuntimeGeneratedFunction{argnames, cache_tag,
338373
context_tag, id, B}) where {
339374
argnames,

0 commit comments

Comments
 (0)