Skip to content

Commit 6fbeffd

Browse files
authored
Ignore identity optic when composing (#96)
1 parent 29cdd41 commit 6fbeffd

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
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.8.2"
6+
version = "0.8.3"
77

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

src/varname.jl

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,14 @@ end
150150

151151
# Allow compositions with optic.
152152
function Base.:(optic::ALLOWED_OPTICS, vn::VarName{sym,<:ALLOWED_OPTICS}) where {sym}
153-
return VarName{sym}(optic getoptic(vn))
153+
vn_optic = getoptic(vn)
154+
if vn_optic == identity
155+
return VarName{sym}(optic)
156+
elseif optic == identity
157+
return vn
158+
else
159+
return VarName{sym}(optic vn_optic)
160+
end
154161
end
155162

156163
Base.hash(vn::VarName, h::UInt) = hash((getsym(vn), getoptic(vn)), h)

test/varname.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,13 @@ end
5353
@testset "compose and opcompose" begin
5454
@test IndexLens(1) @varname(x.a) == @varname(x.a[1])
5555
@test @varname(x.a) IndexLens(1) == @varname(x.a[1])
56+
57+
@test @varname(x) identity == @varname(x)
58+
@test identity @varname(x) == @varname(x)
59+
@test @varname(x.a) identity == @varname(x.a)
60+
@test identity @varname(x.a) == @varname(x.a)
61+
@test @varname(x[1].b) identity == @varname(x[1].b)
62+
@test identity @varname(x[1].b) == @varname(x[1].b)
5663
end
5764

5865
@testset "get & set" begin

0 commit comments

Comments
 (0)