Skip to content

Commit bb5fa01

Browse files
authored
fix varname_..._leaves for other types (#146)
1 parent f3c890f commit bb5fa01

File tree

4 files changed

+20
-1
lines changed

4 files changed

+20
-1
lines changed

HISTORY.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.13.5
2+
3+
Implemented a generic `varname_leaves` and `varname_and_value_leaves` for other unsupported types.
4+
15
## 0.13.4
26

37
Added missing methods for `subsumes(::IndexLens, ::PropertyLens)` and vice versa.

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.13.4"
6+
version = "0.13.5"
77

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

src/varname/leaves.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ function varname_leaves(vn::VarName, x::LinearAlgebra.UpperTriangular)
6565
VarName{getsym(vn)}(Accessors.IndexLens(Tuple(I)) getoptic(vn))
6666
end
6767
end
68+
# This is a default fallback for types that we don't know how to handle.
69+
varname_leaves(vn::VarName, @nospecialize(::Any)) = [vn]
6870

6971
"""
7072
varname_and_value_leaves(vn::VarName, val)
@@ -259,3 +261,5 @@ function varname_and_value_leaves_inner(vn::VarName, x::LinearAlgebra.UpperTrian
259261
for I in CartesianIndices(x) if I[1] <= I[2]
260262
)
261263
end
264+
# This is a default fallback for types that we don't know how to handle.
265+
varname_and_value_leaves_inner(vn::VarName, @nospecialize(x::Any)) = [Leaf(vn, x)]

test/varname.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,5 +423,16 @@ end
423423
(@varname(x.U[2, 2]), x.U[2, 2]),
424424
])
425425
end
426+
427+
@testset "fallback on other types, e.g. string" begin
428+
x = "a string"
429+
@test Set(varname_leaves(@varname(x), x)) == Set([@varname(x)])
430+
@test Set(collect(varname_and_value_leaves(@varname(x), x))) ==
431+
Set([(@varname(x), x)])
432+
x = 2
433+
@test Set(varname_leaves(@varname(x), x)) == Set([@varname(x)])
434+
@test Set(collect(varname_and_value_leaves(@varname(x), x))) ==
435+
Set([(@varname(x), x)])
436+
end
426437
end
427438
end

0 commit comments

Comments
 (0)