Skip to content

[RFC] monkey patch setindex #107

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Dec 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ version = "0.5.2"

[deps]
ConstructionBase = "187b0558-2788-49d3-abe0-74a17ed4e7c9"
Future = "9fa8497b-333b-5362-9e8d-4d0656e87820"
MacroTools = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09"
Requires = "ae029012-a4dd-5104-9daa-d747884805df"

[compat]
ConstructionBase = "0.1, 1.0"
Expand Down
13 changes: 13 additions & 0 deletions src/Setfield.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ __precompile__(true)
module Setfield
using MacroTools
using MacroTools: isstructdef, splitstructdef, postwalk
using Requires: @require

if VERSION < v"1.1-"
using Future: copy!
end

include("setindex.jl")
include("lens.jl")
include("sugar.jl")
include("functionlenses.jl")
Expand All @@ -23,4 +29,11 @@ for n in names(Setfield, all=true)
end
end

function __init__()
@require StaticArrays="90137ffa-7385-5640-81b9-e52037218182" begin
setindex(a::StaticArrays.StaticArray, args...) =
Base.setindex(a, args...)
end
end

end
2 changes: 1 addition & 1 deletion src/lens.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export constructorof


import Base: get
using Base: setindex, getproperty
using Base: getproperty

"""
Lens
Expand Down
22 changes: 22 additions & 0 deletions src/setindex.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Base.@propagate_inbounds function setindex(args...)
Base.setindex(args...)
end

Base.@propagate_inbounds function setindex(xs::AbstractArray, v, I...)
T = promote_type(eltype(xs), typeof(v))
ys = similar(xs, T)
if eltype(xs) !== Union{}
copy!(ys, xs)
end
ys[I...] = v
return ys
end

Base.@propagate_inbounds function setindex(d0::AbstractDict, v, k)
K = promote_type(keytype(d0), typeof(k))
V = promote_type(valtype(d0), typeof(v))
d = empty(d0, K, V)
copy!(d, d0)
d[k] = v
return d
end
2 changes: 1 addition & 1 deletion test/perf.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function lens_set_i((obj, val, i))
end

function hand_set_i((obj, val, i))
@inbounds setindex(obj, val, i)
@inbounds Base.setindex(obj, val, i)
end

function benchmark_lens_vs_hand(b_lens::Benchmark, b_hand::Benchmark)
Expand Down
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module TestSetfield

include("test_setindex.jl")
include("test_examples.jl")
include("test_setmacro.jl")
include("test_core.jl")
Expand Down
37 changes: 37 additions & 0 deletions test/test_setindex.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
module TestSetindex
using Setfield
using Test

"""
==ₜ(x, y)

Check that _type_ and value of `x` and `y` are equal.
"""
==ₜ(_, _) = false
==ₜ(x::T, y::T) where T = x == y

@testset "==ₜ" begin
@test 1 ==ₜ 1
@test !(1.0 ==ₜ 1)
end

@testset "setindex" begin
arr = [1,2,3]
@test_throws MethodError Base.setindex(arr, 10, 1)
@test Setfield.setindex(arr, 10, 1) == [10, 2, 3]
@test arr == [1,2,3]
@test @set(arr[1] = 10) == [10, 2, 3]
@test arr == [1,2,3]
@test Setfield.setindex(arr, 10.0, 1) ==ₜ Float64[10.0, 2.0, 3.0]

d = Dict(:a => 1, :b => 2)
@test_throws MethodError Base.setindex(d, 10, :a)
@test Setfield.setindex(d, 10, :a) == Dict(:a=>10, :b=>2)
@test d == Dict(:a => 1, :b => 2)
@test @set(d[:a] = 10) == Dict(:a=>10, :b=>2)
@test d == Dict(:a => 1, :b => 2)
@test Setfield.setindex(d, 30, "c") ==ₜ Dict(:a=>1, :b=>2, "c"=>30)
@test Setfield.setindex(d, 10.0, :a) ==ₜ Dict(:a=>10.0, :b=>2.0)
end

end