Skip to content

Commit e57ebca

Browse files
torfjeldeyebai
andcommitted
Fix concretization for varname (#75)
Use `Setfield.need_dynamic_lens` as the default value for `concretize` in `varname`. This way we preserve pre-0.6 behavior, thus not make the 0.6-release "less" breaking. Ref: TuringLang/DynamicPPL.jl#440 Co-authored-by: Hong Ge <[email protected]>
1 parent 1158f16 commit e57ebca

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ uuid = "7a57a42e-76ec-4ea3-a279-07e840d6d9cf"
33
keywords = ["probablistic programming"]
44
license = "MIT"
55
desc = "Common interfaces for probabilistic programming"
6-
version = "0.6"
6+
version = "0.6.1"
77

88
[deps]
99
AbstractMCMC = "80f14c24-f653-4e6a-9b94-39d6b0f70001"

src/varname.jl

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,8 @@ end
414414

415415
subsumes_index(i::Colon, ::Colon) = error("Colons cannot be subsumed")
416416
subsumes_index(i, ::Colon) = error("Colons cannot be subsumed")
417+
# Necessary to avoid ambiguity errors.
418+
subsumes_index(::AbstractVector, ::Colon) = error("Colons cannot be subsumed")
417419
subsumes_index(i::Colon, j) = true
418420
subsumes_index(i::AbstractVector, j) = issubset(j, i)
419421
subsumes_index(i, j) = i == j
@@ -520,11 +522,11 @@ julia> x = (a = [1.0 2.0; 3.0 4.0; 5.0 6.0], );
520522
julia> @varname(x.a[1:end, end][:], true)
521523
x.a[1:3,2][:]
522524
523-
julia> @varname(x.a[end])
525+
julia> @varname(x.a[end], false) # disable concretization
524526
ERROR: LoadError: Variable name `x.a[end]` is dynamic and requires concretization!
525527
[...]
526528
527-
julia> @varname(x.a[end], true)
529+
julia> @varname(x.a[end]) # concretization occurs by default if deemed necessary
528530
x.a[6]
529531
530532
julia> # Note that "dynamic" here refers to usage of `begin` and/or `end`,
@@ -575,12 +577,13 @@ julia> x = (a = [(b = rand(2), )], ); getlens(@varname(x.a[1].b[end], true))
575577
Using `begin` in an indexing expression to refer to the first index requires at least
576578
Julia 1.5.
577579
"""
578-
macro varname(expr::Union{Expr,Symbol}, concretize::Bool=false)
580+
macro varname(expr::Union{Expr,Symbol}, concretize::Bool=Setfield.need_dynamic_lens(expr))
579581
return varname(expr, concretize)
580582
end
581583

582-
varname(sym::Symbol, concretize=false) = :($(AbstractPPL.VarName){$(QuoteNode(sym))}())
583-
function varname(expr::Expr, concretize=false)
584+
varname(sym::Symbol) = :($(AbstractPPL.VarName){$(QuoteNode(sym))}())
585+
varname(sym::Symbol, _) = varname(sym)
586+
function varname(expr::Expr, concretize=Setfield.need_dynamic_lens(expr))
584587
if Meta.isexpr(expr, :ref) || Meta.isexpr(expr, :.)
585588
# Split into object/base symbol and lens.
586589
sym_escaped, lens = Setfield.parse_obj_lens(expr)

0 commit comments

Comments
 (0)