Skip to content

Commit

Permalink
remove support for composite lens
Browse files Browse the repository at this point in the history
  • Loading branch information
sunxd3 committed Mar 31, 2024
1 parent 9caa4dc commit dca1020
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 30 deletions.
26 changes: 4 additions & 22 deletions src/varname.jl
Original file line number Diff line number Diff line change
Expand Up @@ -670,28 +670,10 @@ function _parse_obj_optic(ex)
obj, optic
end

# Accessors doesn't have the same support for interpolation, so copy and modify Setfield's parsing functions
is_interpolation(x) = x isa Expr && x.head == :$

function _parse_obj_optics_composite(lensexprs::Vector)
if isempty(lensexprs)
return esc(:_), ()
else
obj, outermostlens = _parse_obj_optics(lensexprs[1])
innerlenses = map(lensexprs[2:end]) do innerex
o, lens = _parse_obj_optics(innerex)
@assert o == esc(:_)
lens
end
return obj, (outermostlens, innerlenses...)
end
end

# Accessors doesn't have the same support for interpolation
# so this function is copied and altered from `Setfield._parse_obj_lens`
function _parse_obj_optics(ex)
if Meta.isexpr(ex, :call) && ex.args[1] == : # matching ∘(opticsexprs)
opticsexprs = ex.args[2:end]
return _parse_obj_optics_composite(opticsexprs)
elseif is_interpolation(ex)
if ex isa Expr && ex.head == :$
@assert length(ex.args) == 1
return esc(:_), (esc(ex.args[1]),)
elseif Meta.isexpr(ex, :ref)
Expand All @@ -713,7 +695,7 @@ function _parse_obj_optics(ex)
obj, frontoptics = _parse_obj_optics(front)
if property isa Union{Symbol,String}
optics = :($(Accessors.PropertyLens){$(QuoteNode(property))}())
elseif is_interpolation(property)
elseif property isa Expr && property.head == :$
optics = :($(Accessors.PropertyLens){$(esc(property.args[1]))}())
else
throw(ArgumentError(
Expand Down
8 changes: 0 additions & 8 deletions test/varname.jl
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,6 @@ end

end

@testset "parsing composite optics" begin
ex = :((_.f, _[1]))
obj, optics = AbstractPPL._parse_obj_optics(ex)
@test obj == :($(Expr(:escape, :_)))
@test optics[1] == (:(($(Accessors.PropertyLens)){:f}()),)
@test optics[2] == (:(($(Accessors.IndexLens))($(Expr(:escape, :((1,)))))),)
end

@testset "type stability" begin
@inferred VarName{:a}()
@inferred VarName{:a}(IndexLens(1))
Expand Down

0 comments on commit dca1020

Please sign in to comment.