Skip to content

RFC: setindex for mutable collections #33495

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

Closed
wants to merge 6 commits into from
Closed
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
35 changes: 35 additions & 0 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -834,6 +834,41 @@ function setindex!(A::Array{T}, X::Array{T}, c::Colon) where T
return A
end

"""
setindex(collection, value, key...)

Create a new collection with the element/elements of the location specified by `key`
replaced with `value`(s).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also mention that the new collection may have a different element type, as determined by promote_type.


# Implementation

`setindex(collection, value, key...)` must have the property such that

```julia
y1 = setindex(x, value, key...)

y2 = copy′(x)
@assert convert.(eltype(y2), x) == y2

y2[key...] = value
@assert convert.(eltype(y2), y1) == y2
```

with a suitable definition of `copy′` such that `y2[key...] = value` succeeds.

`setindex` should support more combinations of arguments by widening collection
type as required.
"""
function setindex end

function setindex(xs::AbstractArray, v, I...)
T = promote_type(eltype(xs), typeof(v))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we use promote_typejoin instead?

ATM, eltype(setindex([0, 0], 0.5, 1)) is Float64. It'd be Real if we use promote_typejoin. The former is compatible with how vcat work and the latter is compatible with how collect and broadcast work.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd say promote_type is the right choice. Otherwise you'd very often get abstract element types, which really kill performance.

ys = similar(xs, T)
copy!(ys, xs)
ys[I...] = v
return ys
end

# efficiently grow an array

_growbeg!(a::Vector, delta::Integer) =
Expand Down
9 changes: 9 additions & 0 deletions base/dict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,15 @@ function setindex!(h::Dict{K,V}, v0, key::K) where V where K
return h
end

function setindex(d0::Dict, v, k)
K = promote_type(keytype(d0), typeof(k))
V = promote_type(valtype(d0), typeof(v))
d = Dict{K, V}()
copy!(d, d0)
d[k] = v
return d
end

"""
get!(collection, key, default)

Expand Down
14 changes: 14 additions & 0 deletions test/arrayops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2651,6 +2651,20 @@ end
# Throws ArgumentError for negative dimensions in Array
@test_throws ArgumentError fill('a', -10)

@testset "setindex" begin
==ₜ(_, _) = false
==ₜ(x::T, y::T) where T = x == y
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

==ₜ seems so handy, maybe it should actually live in e.g. Test?


@test @inferred(Base.setindex(Int[1, 2], 3.0, 2)) ==ₜ [1.0, 3.0]
@test @inferred(Base.setindex(Int[1, 2, 3], :two, 2)) ==ₜ [1, :two, 3]

@testset "no mutation" begin
arr = [1]
@test Base.setindex(arr, 2, 1) ==ₜ [2]
@test arr == [1]
end
end

@testset "Issue 33919" begin
A = Array[rand(2, 3), rand(3, 1)]
B = Array[rand(2, 2), rand(1, 4)]
Expand Down
18 changes: 18 additions & 0 deletions test/dict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1072,3 +1072,21 @@ end
@test testdict[:b] == 1
end
end

@testset "setindex" begin
==ₜ(_, _) = false
==ₜ(x::T, y::T) where T = x == y

@test @inferred(Base.setindex(Dict(:a=>1, :b=>2), 10, :a)) ==ₜ
Dict(:a=>10, :b=>2)
@test @inferred(Base.setindex(Dict(:a=>1, :b=>2), 3, "c")) ==ₜ
Dict(:a=>1, :b=>2, "c"=>3)
@test @inferred(Base.setindex(Dict(:a=>1, :b=>2), 3.0, :c)) ==ₜ
Dict(:a=>1.0, :b=>2.0, :c=>3.0)

@testset "no mutation" begin
dict = Dict(:a=>1, :b=>2)
@test Base.setindex(dict, 10, :a) ==ₜ Dict(:a=>10, :b=>2)
@test dict == Dict(:a=>1, :b=>2)
end
end