300300Return `model` but with all random variables prefixed by `x`.
301301
302302If `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"""
304320prefix (model:: Model , x) = contextualize (model, PrefixContext {Symbol(x)} (model. context))
305321function prefix (model:: Model , :: Val{x} ) where {x}
@@ -314,9 +330,19 @@ Return `model` but with all random variables prefixed by `prefix_expr`.
314330The 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"""
321347macro prefix (model, prefix_expr)
322348 return :($ prefix ($ (esc (model)), $ Val {$Symbol($(esc(prefix_expr)))} ()))
0 commit comments