Skip to content

Commit

Permalink
deprecate ConstIndexLens
Browse files Browse the repository at this point in the history
  • Loading branch information
jw3126 committed Jan 13, 2020
1 parent 32a72d9 commit bef898c
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 42 deletions.
3 changes: 2 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
37 changes: 16 additions & 21 deletions src/lens.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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...]

Expand Down
37 changes: 19 additions & 18 deletions test/test_core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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(_))
Expand Down Expand Up @@ -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 _
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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]
Expand Down
5 changes: 3 additions & 2 deletions test/test_setmacro.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ using Test
using .Clone: Clone

using StaticArrays: @SMatrix
using StaticNumbers

@testset "setmacro, lensmacro isolation" begin

Expand All @@ -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)
Expand Down

0 comments on commit bef898c

Please sign in to comment.