Skip to content

Commit a80a0f2

Browse files
sunxd3devmotion
andauthored
Bring back function between varname and optic (#95)
* add `∘` and `⨟` function between varname and optic * Apply suggestions from code review Co-authored-by: David Widmann <devmotion@users.noreply.github.com> --------- Co-authored-by: David Widmann <devmotion@users.noreply.github.com>
1 parent 9d35b63 commit a80a0f2

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

src/varname.jl

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,10 @@ function Accessors.set(obj, vn::VarName{sym}, value) where {sym}
148148
return Accessors.set(obj, PropertyLens{sym}() getoptic(vn), value)
149149
end
150150

151+
# Allow compositions with optic.
152+
function Base.:(optic::ALLOWED_OPTICS, vn::VarName{sym,<:ALLOWED_OPTICS}) where {sym}
153+
return VarName{sym}(optic getoptic(vn))
154+
end
151155

152156
Base.hash(vn::VarName, h::UInt) = hash((getsym(vn), getoptic(vn)), h)
153157
function Base.:(==)(x::VarName, y::VarName)
@@ -631,8 +635,8 @@ function varname(expr::Expr, concretize=Accessors.need_dynamic_optic(expr))
631635
if concretize
632636
return :(
633637
$(AbstractPPL.VarName){$sym}(
634-
$(AbstractPPL.concretize)($optics, $sym_escaped)
635-
)
638+
$(AbstractPPL.concretize)($optics, $sym_escaped)
639+
)
636640
)
637641
elseif Accessors.need_dynamic_optic(expr)
638642
error("Variable name `$(expr)` is dynamic and requires concretization!")
@@ -687,7 +691,7 @@ function _parse_obj_optics(ex)
687691
else
688692
throw(ArgumentError(
689693
string("Error while parsing :($ex). Second argument to `getproperty` can only be",
690-
"a `Symbol` or `String` literal, received `$property` instead.")
694+
"a `Symbol` or `String` literal, received `$property` instead.")
691695
))
692696
end
693697
else

test/varname.jl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ using OffsetArrays
55
using AbstractPPL: , , , ,
66

77
using AbstractPPL: Accessors
8-
using AbstractPPL.Accessors: IndexLens, PropertyLens
8+
using AbstractPPL.Accessors: IndexLens, PropertyLens,
99

1010
macro test_strict_subsumption(x, y)
1111
quote
@@ -50,6 +50,11 @@ end
5050
@test test_equal(@varname(x.a[1:end, end][:], true), @varname(x.a[1:3,2][1:3]))
5151
end
5252

53+
@testset "compose and opcompose" begin
54+
@test IndexLens(1) @varname(x.a) == @varname(x.a[1])
55+
@test @varname(x.a) IndexLens(1) == @varname(x.a[1])
56+
end
57+
5358
@testset "get & set" begin
5459
x = (a = [1.0 2.0; 3.0 4.0; 5.0 6.0], b = 1.0);
5560
@test get(x, @varname(a[1, 2])) == 2.0

0 commit comments

Comments
 (0)