Skip to content

Commit 65d2451

Browse files
Merge pull request #27 from c42f/cjf/constructor-docs
Improve README documentation for constructors
2 parents 0e2c552 + 94e7222 commit 65d2451

File tree

1 file changed

+65
-2
lines changed

1 file changed

+65
-2
lines changed

README.md

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ Note that `RuntimeGeneratedFunction` does not handle closures. Please use the
1111
package for more fixable staged programming. While `GeneralizedGenerated.jl` is
1212
more powerful, `RuntimeGeneratedFunctions.jl` handles large expressions better.
1313

14-
Credit to Chris Foster (@c4tf) for the implementation idea.
14+
## Simple Example
1515

16-
## Example
16+
Here's an example showing how to construct and immediately call a runtime
17+
generated function:
1718

1819
```julia
1920
using RuntimeGeneratedFunctions
@@ -34,3 +35,65 @@ function no_worldage()
3435
end
3536
no_worldage()
3637
```
38+
39+
## Changing how global symbols are looked up
40+
41+
If you want to use helper functions or global variables from a different
42+
module within your function expression you'll need to pass a `context_module`
43+
to the `@RuntimeGeneratedFunction` constructor. For example
44+
45+
```julia
46+
RuntimeGeneratedFunctions.init(@__MODULE__)
47+
48+
module A
49+
using RuntimeGeneratedFunctions
50+
RuntimeGeneratedFunctions.init(A)
51+
helper_function(x) = x + 1
52+
end
53+
54+
function g()
55+
expression = :(f(x) = helper_function(x))
56+
# context module is `A` so that `helper_function` can be found.
57+
f = @RuntimeGeneratedFunction(A, expression)
58+
@show f(1)
59+
end
60+
```
61+
62+
## Precompilation and setting the function expression cache
63+
64+
For technical reasons RuntimeGeneratedFunctions needs to cache the function
65+
expression in a global variable within some module. This is normally
66+
transparent to the user, but if the `RuntimeGeneratedFunction` is evaluated
67+
during module precompilation, the cache module must be explicitly set to the
68+
module currently being precompiled. This is relevant for helper functions in
69+
some module which construct a RuntimeGeneratedFunction on behalf of the user.
70+
For example, in the following code, any third party user of
71+
`HelperModule.construct_rgf()` user needs to pass their own module as the
72+
`cache_module` if they want the returned function to work after precompilation:
73+
74+
```julia
75+
RuntimeGeneratedFunctions.init(@__MODULE__)
76+
77+
# Imagine HelperModule is in a separate package and will be precompiled
78+
# separately.
79+
module HelperModule
80+
using RuntimeGeneratedFunctions
81+
RuntimeGeneratedFunctions.init(HelperModule)
82+
83+
function construct_rgf(cache_module, context_module, ex)
84+
ex = :((x)->$ex^2 + x)
85+
RuntimeGeneratedFunction(cache_module, context_module, ex)
86+
end
87+
end
88+
89+
function g()
90+
ex = :(x + 1)
91+
# Here cache_module is set to the module currently being compiled so that
92+
# the returned RGF works with Julia's module precompilation system.
93+
HelperModule.construct_rgf(@__MODULE__, @__MODULE__, ex)
94+
end
95+
96+
f = g()
97+
@show f(1)
98+
```
99+

0 commit comments

Comments
 (0)