From bef898c917493da530fdc24700502b041f0b8b7c Mon Sep 17 00:00:00 2001 From: Jan Weidner Date: Sun, 12 Jan 2020 22:37:32 +0100 Subject: [PATCH] deprecate ConstIndexLens --- Project.toml | 3 ++- src/lens.jl | 37 ++++++++++++++++--------------------- test/test_core.jl | 37 +++++++++++++++++++------------------ test/test_setmacro.jl | 5 +++-- 4 files changed, 40 insertions(+), 42 deletions(-) diff --git a/Project.toml b/Project.toml index 6258bca..a45beff 100644 --- a/Project.toml +++ b/Project.toml @@ -20,7 +20,8 @@ InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240" Kwonly = "18d08c8c-0732-55ee-a446-91a51d7b4206" QuickTypes = "ae2dfa86-617c-530c-b392-ef20fdad97bb" StaticArrays = "90137ffa-7385-5640-81b9-e52037218182" +StaticNumbers = "c5e4b96a-f99f-5557-8ed2-dc63ef9b5131" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" [targets] -test = ["Test", "Kwonly", "QuickTypes", "StaticArrays", "BenchmarkTools", "InteractiveUtils"] +test = ["Test", "Kwonly", "QuickTypes", "StaticArrays", "BenchmarkTools", "InteractiveUtils", "StaticNumbers"] diff --git a/src/lens.jl b/src/lens.jl index 23f27fe..4be55e3 100644 --- a/src/lens.jl +++ b/src/lens.jl @@ -170,30 +170,25 @@ Base.@propagate_inbounds function set(obj, l::IndexLens, val) setindex(obj, val, l.indices...) end -""" - ConstIndexLens{I} - -Lens with index stored in type parameter. This is useful for type-stable -[`get`](@ref) and [`set`](@ref) operations on tuples and named tuples. - -This lens can be constructed by, e.g., `@lens _[\$1]`. Complex expression -must be wrapped with `\$(...)` like `@lens _[\$(length(xs))]`. - -# Examples -```jldoctest -julia> using Setfield - -julia> get((1, 2.0), @lens _[\$1]) -1 - -julia> Base.promote_op(get, typeof.(((1, 2.0), @lens _[\$1]))...) -Int64 +MSG_CONST_INDEX_LENS = """ +ConstIndexLens is deprecate. Replace as follows: +```julia +# old +@set obj[\$1] = 2 -julia> Base.promote_op(get, typeof.(((1, 2.0), @lens _[1]))...) !== Int -true +# new +using StaticNumbers +@set obj[static(1)] = 2 ``` """ -struct ConstIndexLens{I} <: Lens end + +@doc MSG_CONST_INDEX_LENS -> +struct ConstIndexLens{I} <: Lens + function ConstIndexLens{I}() where {I} + Base.depwarn(MSG_CONST_INDEX_LENS, :ConstIndexLens) + new{I}() + end +end Base.@propagate_inbounds get(obj, ::ConstIndexLens{I}) where I = obj[I...] diff --git a/test/test_core.jl b/test/test_core.jl index b29479a..336541e 100644 --- a/test/test_core.jl +++ b/test/test_core.jl @@ -3,7 +3,8 @@ using Test using Setfield using Setfield: compose, get_update_op using Setfield.Experimental -import ConstructionBase +using ConstructionBase: ConstructionBase +using StaticNumbers: static struct T a @@ -110,10 +111,10 @@ end se1 = @set t.a[end-1] = 10 @test s1 === se1 - s1 = @set t.a[$1] = 10 + s1 = @set t.a[static(1)] = 10 @test s1 === T((10,2),(3,4)) i = 1 - si = @set t.a[$i] = 10 + si = @set t.a[static(i)] = 10 @test s1 === si t = @set T(1,2).a = 2 @@ -139,9 +140,9 @@ Base.show(io::IO, ::MIME"text/plain", ::LensWithTextPlain) = @lens _[1] @lens _[:a] @lens _["a"] - @lens _[$1] - @lens _[$1, $(1 + 1)] - @lens _.a.b[:c]["d"][2][$3] + @lens _[static(1)] + @lens _[static(1), static(1 + 1)] + @lens _.a.b[:c]["d"][2][static(3)] @lens _ @lens first(_) @lens last(first(_)) @@ -194,8 +195,8 @@ end @lens _.b.a @lens _.b.a.b[2] @lens _.b.a.b[i] - @lens _.b.a.b[$2] - @lens _.b.a.b[$i] + @lens _.b.a.b[static(2)] + @lens _.b.a.b[static(i)] @lens _.b.a.b[end] @lens _.b.a.b[identity(end) - 1] @lens _ @@ -229,10 +230,10 @@ end ((@lens _.b.a ), o21), ((@lens _.b.a.b[2] ), 4 ), ((@lens _.b.a.b[i+1] ), 4 ), - ((@lens _.b.a.b[$2] ), 4 ), - ((@lens _.b.a.b[$(i+1)]), 4 ), - ((@lens _.b.a.b[$2] ), 4.0), - ((@lens _.b.a.b[$(i+1)]), 4.0), + ((@lens _.b.a.b[static(2)] ), 4 ), + ((@lens _.b.a.b[static((i+1))]), 4 ), + ((@lens _.b.a.b[static(2)] ), 4.0), + ((@lens _.b.a.b[static((i+1))]), 4.0), ((@lens _.b.a.b[end]), 4.0), ((@lens _.b.a.b[end÷2+1]), 4.0), ((@lens _ ), obj), @@ -292,26 +293,26 @@ end @test set(obj, l, true) == (a=(1, (a=10, b=true), 3), b=4) end -@testset "ConstIndexLens" begin +@testset "StaticNumbers" begin obj = (1, 2.0, '3') - l = @lens _[$1] + l = @lens _[static(1)] @test (@inferred get(obj, l)) === 1 @test (@inferred set(obj, l, 6.0)) === (6.0, 2.0, '3') - l = @lens _[$(1 + 1)] + l = @lens _[static(1 + 1)] @test (@inferred get(obj, l)) === 2.0 @test (@inferred set(obj, l, 6)) === (1, 6, '3') n = 1 - l = @lens _[$(3n)] + l = @lens _[static(3n)] @test (@inferred get(obj, l)) === '3' @test (@inferred set(obj, l, 6)) === (1, 2.0, 6) - l = @lens _[$(1:3)] + l = @lens _[static(1):static(3)] @test get([4,5,6,7], l) == [4,5,6] @testset "complex example (sweeper)" begin sweeper_with_const = ( model = (1, 2.0, 3im), - axis = (@lens _[$2]), + axis = (@lens _[static(2)]), ) sweeper_with_noconst = @set sweeper_with_const.axis = @lens _[2] diff --git a/test/test_setmacro.jl b/test/test_setmacro.jl index 3a7a25d..bc48fa3 100644 --- a/test/test_setmacro.jl +++ b/test/test_setmacro.jl @@ -18,6 +18,7 @@ using Test using .Clone: Clone using StaticArrays: @SMatrix +using StaticNumbers @testset "setmacro, lensmacro isolation" begin @@ -27,8 +28,8 @@ using StaticArrays: @SMatrix @test Clone.@lens(_[1] ) isa Setfield.Lens @test Clone.@lens(first(_) ) isa Setfield.Lens @test Clone.@lens(_[end] ) isa Setfield.Lens - @test Clone.@lens(_[$1] ) isa Setfield.Lens - @test Clone.@lens(_.a[1][end, end-2].b[$1, $1]) isa Setfield.Lens + @test Clone.@lens(_[static(1)] ) isa Setfield.Lens + @test Clone.@lens(_.a[1][end, end-2].b[static(1), static(1)]) isa Setfield.Lens @test Setfield.@lens(_.a) === Clone.@lens(_.a) @test Setfield.@lens(_.a.b) === Clone.@lens(_.a.b)