@@ -22,16 +22,32 @@ function rand_macro(ex)
2222 :(rng:: AbstractRNG ),
2323 as_sampler (ex. args[1 ]. args[2 ], istrivial)),
2424 body)
25- istrivial && return ex
26- sp = quote
27- Random. Sampler (:: Type{RNG} , n:: Repetition ) where {RNG<: AbstractRNG } =
28- SamplerSimple ($ name, tuple (SP))
25+
26+ sp = if istrivial
27+ # we explicitly define Sampler even in the trivial case to handle
28+ # redefinitions, where the old rand/sampler pair (for SamplerSimple)
29+ # is overwritten by a new one (for SamplerTrivial)
30+ quote
31+ Random. Sampler (:: Type{RNG} , n:: Repetition ) where {RNG<: AbstractRNG } =
32+ SamplerTrivial ($ name)
33+ end
34+ else
35+ quote
36+ Random. Sampler (:: Type{RNG} , n:: Repetition ) where {RNG<: AbstractRNG } =
37+ SamplerSimple ($ name, tuple (SP))
38+ end
2939 end
40+
3041 # insert x::X in the argument list, between RNG and n::Repetition
3142 insert! (sp. args[2 ]. args[1 ]. args[1 ]. args, 3 , namefull)
32- SP = [Expr (:call , :Sampler , :RNG , x, :n ) for x in sps]
33- @assert :SP == pop! (sp. args[2 ]. args[2 ]. args[2 ]. args[3 ]. args)
34- append! (sp. args[2 ]. args[2 ]. args[2 ]. args[3 ]. args, SP)
43+
44+ # insert inner samplers
45+ if ! istrivial
46+ SP = [Expr (:call , :Sampler , :RNG , x, :n ) for x in sps]
47+ @assert :SP == pop! (sp. args[2 ]. args[2 ]. args[2 ]. args[3 ]. args)
48+ append! (sp. args[2 ]. args[2 ]. args[2 ]. args[3 ]. args, SP)
49+ end
50+
3551 quote
3652 $ ex
3753 $ (sp. args[2 ]) # unwrap the quote/block around the definition
0 commit comments