From 8dda9c9d92ff27cb2f5fc32cf32c62e082465479 Mon Sep 17 00:00:00 2001 From: Simeon Schaub Date: Fri, 5 Feb 2021 19:04:47 +0100 Subject: [PATCH 1/3] clarify documentation for at-view --- base/views.jl | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/base/views.jl b/base/views.jl index ccf24d4cdea3a..378ed8bb3cfa6 100644 --- a/base/views.jl +++ b/base/views.jl @@ -78,8 +78,9 @@ end @view A[inds...] Creates a `SubArray` from an indexing expression. This can only be applied directly to a -reference expression (e.g. `@view A[1,2:end]`), and should *not* be used as the target of -an assignment (e.g. `@view(A[1,2:end]) = ...`). See also [`@views`](@ref) +reference expression (e.g. `@view A[1, 2:end]`). It should *not* be used as the target of +a regular assignment (e.g. `@view(A[1, 2:end]) = ...`), although it can still be useful for +broadcasted assignment (e.g. `@view(A[1, 2:end] .*= 2`). See also [`@views`](@ref) to switch an entire block of code to use views for slicing. !!! compat "Julia 1.5" From 64ab84925918193eca4fc760becf8cc755b291b1 Mon Sep 17 00:00:00 2001 From: Simeon Schaub Date: Fri, 5 Feb 2021 19:08:54 +0100 Subject: [PATCH 2/3] fix typo --- base/views.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/base/views.jl b/base/views.jl index 378ed8bb3cfa6..aee9710f5bc30 100644 --- a/base/views.jl +++ b/base/views.jl @@ -80,7 +80,7 @@ end Creates a `SubArray` from an indexing expression. This can only be applied directly to a reference expression (e.g. `@view A[1, 2:end]`). It should *not* be used as the target of a regular assignment (e.g. `@view(A[1, 2:end]) = ...`), although it can still be useful for -broadcasted assignment (e.g. `@view(A[1, 2:end] .*= 2`). See also [`@views`](@ref) +broadcasted assignment (e.g. `@view(A[1, 2:end]) .*= 2`). See also [`@views`](@ref) to switch an entire block of code to use views for slicing. !!! compat "Julia 1.5" From 46cf562e8be8318e7d377e75011b0e37dd02f56e Mon Sep 17 00:00:00 2001 From: Simeon Schaub Date: Tue, 9 Feb 2021 16:49:56 +0100 Subject: [PATCH 3/3] Apply suggestions from code review Co-authored-by: Matt Bauman --- base/views.jl | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/base/views.jl b/base/views.jl index aee9710f5bc30..f60dc04094a43 100644 --- a/base/views.jl +++ b/base/views.jl @@ -77,11 +77,23 @@ end """ @view A[inds...] -Creates a `SubArray` from an indexing expression. This can only be applied directly to a -reference expression (e.g. `@view A[1, 2:end]`). It should *not* be used as the target of -a regular assignment (e.g. `@view(A[1, 2:end]) = ...`), although it can still be useful for -broadcasted assignment (e.g. `@view(A[1, 2:end]) .*= 2`). See also [`@views`](@ref) -to switch an entire block of code to use views for slicing. +Transform the indexing expression `A[inds...]` into the equivalent [`view`](@ref) call. + +This can only be applied directly to a single indexing expression and is particularly +helpful for expressions that include the special `begin` or `end` indexing syntaxes +like `A[begin, 2:end-1]` (as those are not supported by the normal [`view`](@ref) +function). + +Note that `@view` cannot be used as the target of a regular assignment (e.g., +`@view(A[1, 2:end]) = ...`), nor would the un-decorated +[indexed assignment](@ref man-indexed-assignment) (`A[1, 2:end] = ...`) +or broadcasted indexed assignment (`A[1, 2:end] .= ...`) make a copy. It can be useful, +however, for _updating_ broadcasted assignments like `@view(A[1, 2:end]) .+= 1` +because this is a simple syntax for `@view(A[1, 2:end]) .= @view(A[1, 2:end]) + 1`, +and the indexing expression on the right-hand side would otherwise make a +copy without the `@view`. + +See also [`@views`](@ref) to switch an entire block of code to use views for non-scalar indexing. !!! compat "Julia 1.5" Using `begin` in an indexing expression to refer to the first index requires at least