Skip to content

Commit 4f98dc1

Browse files
Moelfnalimilantimholy
authored andcommitted
mention getindex() shouldn't return a "view" (#47099)
fix #47078 --------- Co-authored-by: Milan Bouchet-Valat <[email protected]> Co-authored-by: Tim Holy <[email protected]>
1 parent 5c64a46 commit 4f98dc1

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

base/abstractarray.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1256,6 +1256,11 @@ end
12561256
12571257
Return a subset of array `A` as specified by `inds`, where each `ind` may be,
12581258
for example, an `Int`, an [`AbstractRange`](@ref), or a [`Vector`](@ref).
1259+
1260+
When `inds` selects multiple elements, this function returns a newly
1261+
allocated array. To index multiple elements without making a copy,
1262+
use [`view`](@ref) instead.
1263+
12591264
See the manual section on [array indexing](@ref man-array-indexing) for details.
12601265
12611266
# Examples

doc/src/manual/interfaces.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,10 @@ julia> collect(Iterators.reverse(Squares(4)))
152152

153153
| Methods to implement | Brief description |
154154
|:-------------------- |:-------------------------------- |
155-
| `getindex(X, i)` | `X[i]`, indexed element access |
156-
| `setindex!(X, v, i)` | `X[i] = v`, indexed assignment |
155+
| `getindex(X, i)` | `X[i]`, indexed access, non-scalar `i` should allocate a copy |
156+
| `setindex!(X, v, i)` | `X[i] = v`, indexed assignment |
157157
| `firstindex(X)` | The first index, used in `X[begin]` |
158-
| `lastindex(X)` | The last index, used in `X[end]` |
158+
| `lastindex(X)` | The last index, used in `X[end]` |
159159

160160
For the `Squares` iterable above, we can easily compute the `i`th element of the sequence by squaring
161161
it. We can expose this as an indexing expression `S[i]`. To opt into this behavior, `Squares`

0 commit comments

Comments
 (0)