-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
RFC: setindex for mutable collections #33495
Changes from 4 commits
b64dc59
742cc7c
4743df1
9f9065a
b204b6c
b57088b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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). | ||
|
||
# Implementation | ||
|
||
`setindex(collection, value, key...)` must have the property such that | ||
|
||
```julia | ||
nalimilan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we use ATM, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd say |
||
ys = similar(xs, T) | ||
copy!(ys, xs) | ||
ys[I...] = v | ||
return ys | ||
end | ||
|
||
# efficiently grow an array | ||
|
||
_growbeg!(a::Vector, delta::Integer) = | ||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -2651,6 +2651,14 @@ 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 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||
|
||||||
tkf marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
@test Base.setindex(Int[1, 2], 3.0, 2) ==ₜ [1.0, 3.0] | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it is good to have There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the point since that doesn't make any difference? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. emphasis |
||||||
@test Base.setindex(Int[1, 2, 3], :two, 2) ==ₜ [1, :two, 3] | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
end | ||||||
|
||||||
@testset "Issue 33919" begin | ||||||
A = Array[rand(2, 3), rand(3, 1)] | ||||||
B = Array[rand(2, 2), rand(1, 4)] | ||||||
|
There was a problem hiding this comment.
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
.