Skip to content

Commit

Permalink
add constructorof for PropertyLens
Browse files Browse the repository at this point in the history
  • Loading branch information
aplavin committed Jul 22, 2024
1 parent 18d8625 commit 3903a66
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/optics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,8 @@ The second constructor may not be type stable when `fieldname` is not a constant
"""
@inline PropertyLens(fieldname) = PropertyLens{fieldname}()

ConstructionBase.constructorof(::Type{PropertyLens{P}}) where {P} = PropertyLens{P}

function (l::PropertyLens{field})(obj) where {field}
getproperty(obj, field)
end
Expand Down
7 changes: 6 additions & 1 deletion test/test_optics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,14 @@ end
@inferred modify(x -> 0, arr, @optic _ |> Elements() |> If(iseven))
end

@testset "convenience constructors" begin
@testset "constructors" begin
@test IndexLens(1, 2, 3) === IndexLens((1, 2, 3))
@test PropertyLens(:a) === PropertyLens{:a}()

f = PropertyLens(:a)
@test constructorof(typeof(f))(Accessors.getfields(f)...) === f
f = IndexLens(1, 2, 3)
@test constructorof(typeof(f))(Accessors.getfields(f)...) === f
end

@testset "broadcasting" begin
Expand Down

0 comments on commit 3903a66

Please sign in to comment.