Skip to content

Commit ce5bd1c

Browse files
clarify documentation for @view (#39542)
* clarify documentation for at-view * fix typo * Apply suggestions from code review Co-authored-by: Matt Bauman <mbauman@juliacomputing.com> Co-authored-by: Matt Bauman <mbauman@juliacomputing.com>
1 parent f13213d commit ce5bd1c

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

base/views.jl

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,23 @@ end
7777
"""
7878
@view A[inds...]
7979
80-
Creates a `SubArray` from an indexing expression. This can only be applied directly to a
81-
reference expression (e.g. `@view A[1,2:end]`), and should *not* be used as the target of
82-
an assignment (e.g. `@view(A[1,2:end]) = ...`). See also [`@views`](@ref)
83-
to switch an entire block of code to use views for slicing.
80+
Transform the indexing expression `A[inds...]` into the equivalent [`view`](@ref) call.
81+
82+
This can only be applied directly to a single indexing expression and is particularly
83+
helpful for expressions that include the special `begin` or `end` indexing syntaxes
84+
like `A[begin, 2:end-1]` (as those are not supported by the normal [`view`](@ref)
85+
function).
86+
87+
Note that `@view` cannot be used as the target of a regular assignment (e.g.,
88+
`@view(A[1, 2:end]) = ...`), nor would the un-decorated
89+
[indexed assignment](@ref man-indexed-assignment) (`A[1, 2:end] = ...`)
90+
or broadcasted indexed assignment (`A[1, 2:end] .= ...`) make a copy. It can be useful,
91+
however, for _updating_ broadcasted assignments like `@view(A[1, 2:end]) .+= 1`
92+
because this is a simple syntax for `@view(A[1, 2:end]) .= @view(A[1, 2:end]) + 1`,
93+
and the indexing expression on the right-hand side would otherwise make a
94+
copy without the `@view`.
95+
96+
See also [`@views`](@ref) to switch an entire block of code to use views for non-scalar indexing.
8497
8598
!!! compat "Julia 1.5"
8699
Using `begin` in an indexing expression to refer to the first index requires at least

0 commit comments

Comments
 (0)