diff --git a/base/subarray.jl b/base/subarray.jl index 7292f351f0485..3b232d896d3c0 100644 --- a/base/subarray.jl +++ b/base/subarray.jl @@ -122,10 +122,18 @@ _maybe_reshape_parent(A::AbstractArray, ::NTuple{N, Bool}) where {N} = reshape(A """ view(A, inds...) -Like [`getindex`](@ref), but returns a view into the parent array `A` with the -given indices instead of making a copy. Calling [`getindex`](@ref) or -[`setindex!`](@ref) on the returned `SubArray` computes the -indices to the parent array on the fly without checking bounds. +Like [`getindex`](@ref), but returns a lightweight array that lazily references +(or is effectively a _view_ into) the parent array `A` at the given index or indices +`inds` instead of eagerly extracting elements or constructing a copied subset. +Calling [`getindex`](@ref) or [`setindex!`](@ref) on the returned value +(often a [`SubArray`](@ref)) computes the indices to access or modify the +parent array on the fly. The behavior is undefined if the shape of the parent array is +changed after `view` is called because there is no bound check for the parent array; e.g., +it may cause a segmentation fault. + +Some immutable parent arrays (like ranges) may choose to simply +recompute a new array in some circumstances instead of returning +a `SubArray` if doing so is efficient and provides compatible semantics. # Examples ```jldoctest @@ -148,6 +156,9 @@ julia> A # Note A has changed even though we modified b 2×2 Matrix{Int64}: 0 2 0 4 + +julia> view(2:5, 2:3) # returns a range as type is immutable +3:4 ``` """ function view(A::AbstractArray, I::Vararg{Any,N}) where {N} diff --git a/doc/src/base/arrays.md b/doc/src/base/arrays.md index 13add145e0858..ec8678361da60 100644 --- a/doc/src/base/arrays.md +++ b/doc/src/base/arrays.md @@ -128,6 +128,7 @@ Base.reinterpret Base.reshape Base.dropdims Base.vec +Base.SubArray ``` ## Concatenation and permutation