diff --git a/src/optics.jl b/src/optics.jl index dee543f..d0c4b63 100644 --- a/src/optics.jl +++ b/src/optics.jl @@ -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 diff --git a/test/test_optics.jl b/test/test_optics.jl index 3c5ac9d..57b5bd6 100644 --- a/test/test_optics.jl +++ b/test/test_optics.jl @@ -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