Skip to content

Commit 5ee727b

Browse files
committed
improved docstring for prefix and @prefix
1 parent 7aef65b commit 5ee727b

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

src/contexts.jl

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,22 @@ end
300300
Return `model` but with all random variables prefixed by `x`.
301301
302302
If `x` is known at compile-time, use `Val{x}()` to avoid runtime overheads for prefixing.
303+
304+
# Examples
305+
306+
```jldoctest
307+
julia> using DynamicPPL: prefix
308+
309+
julia> @model demo() = x ~ Dirac(1)
310+
demo (generic function with 2 methods)
311+
312+
julia> rand(prefix(demo(), :my_prefix))
313+
(var"my_prefix.x" = 1,)
314+
315+
julia> # One can also use `Val` to avoid runtime overheads.
316+
rand(prefix(demo(), Val(:my_prefix)))
317+
(var"my_prefix.x" = 1,)
318+
```
303319
"""
304320
prefix(model::Model, x) = contextualize(model, PrefixContext{Symbol(x)}(model.context))
305321
function prefix(model::Model, ::Val{x}) where {x}
@@ -314,9 +330,19 @@ Return `model` but with all random variables prefixed by `prefix_expr`.
314330
The result of `prefix_expr` must will be converted to a `Symbol` and used as the prefix.
315331
316332
!!! note
317-
This is effectively just a convenience macro for the method [`prefix(::Model, x)`](@ref),
333+
This is effectively just a convenience macro for the method [`DynamicPPL.prefix(::Model, x)`](@ref),
318334
which automatically converts the result of `prefix_expr` into a `Val` to avoid runtime overheads
319335
for static prefixes. For more control over the prefixing, use the method directly.
336+
337+
# Examples
338+
339+
```jldoctest
340+
julia> @model demo() = x ~ Dirac(1)
341+
demo (generic function with 2 methods)
342+
343+
julia> rand(@prefix(demo(), :my_prefix))
344+
(var"my_prefix.x" = 1,)
345+
```
320346
"""
321347
macro prefix(model, prefix_expr)
322348
return :($prefix($(esc(model)), $Val{$Symbol($(esc(prefix_expr)))}()))

0 commit comments

Comments
 (0)