Skip to content

Commit 056be22

Browse files
authored
Modify documentation for view to take into account range special case (#38536)
1 parent 687378e commit 056be22

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

base/subarray.jl

+15-4
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,18 @@ _maybe_reshape_parent(A::AbstractArray, ::NTuple{N, Bool}) where {N} = reshape(A
122122
"""
123123
view(A, inds...)
124124
125-
Like [`getindex`](@ref), but returns a view into the parent array `A` with the
126-
given indices instead of making a copy. Calling [`getindex`](@ref) or
127-
[`setindex!`](@ref) on the returned `SubArray` computes the
128-
indices to the parent array on the fly without checking bounds.
125+
Like [`getindex`](@ref), but returns a lightweight array that lazily references
126+
(or is effectively a _view_ into) the parent array `A` at the given index or indices
127+
`inds` instead of eagerly extracting elements or constructing a copied subset.
128+
Calling [`getindex`](@ref) or [`setindex!`](@ref) on the returned value
129+
(often a [`SubArray`](@ref)) computes the indices to access or modify the
130+
parent array on the fly. The behavior is undefined if the shape of the parent array is
131+
changed after `view` is called because there is no bound check for the parent array; e.g.,
132+
it may cause a segmentation fault.
133+
134+
Some immutable parent arrays (like ranges) may choose to simply
135+
recompute a new array in some circumstances instead of returning
136+
a `SubArray` if doing so is efficient and provides compatible semantics.
129137
130138
# Examples
131139
```jldoctest
@@ -148,6 +156,9 @@ julia> A # Note A has changed even though we modified b
148156
2×2 Matrix{Int64}:
149157
0 2
150158
0 4
159+
160+
julia> view(2:5, 2:3) # returns a range as type is immutable
161+
3:4
151162
```
152163
"""
153164
function view(A::AbstractArray, I::Vararg{Any,N}) where {N}

doc/src/base/arrays.md

+1
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ Base.reinterpret
128128
Base.reshape
129129
Base.dropdims
130130
Base.vec
131+
Base.SubArray
131132
```
132133

133134
## Concatenation and permutation

0 commit comments

Comments
 (0)