Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@view vs @views with scalar indexing #23759

Closed
nalimilan opened this issue Sep 18, 2017 · 2 comments
Closed

@view vs @views with scalar indexing #23759

nalimilan opened this issue Sep 18, 2017 · 2 comments
Labels
arrays [a, r, r, a, y, s]

Comments

@nalimilan
Copy link
Member

Currently @view always returns a SubArray, even when indexing with a scalar:

julia> x = 1:3
1:3

julia> @view x[1]
0-dimensional view(::UnitRange{Int64}, 1) with eltype Int64:
1

OTOH, @views returns the element when indexing with a scalar (as documented):

julia> @views x[1]
1

The behavior of @views sounds more consistent with plain getindex, and generally more useful. Rather than changing @view, @mbauman suggested deprecating it in favor of @views.

See #23051 (comment) for an example where the behavior of @views was more natural (but participant to the discussion seemed to be aware of its existence, so we ended up checking the index type manually).

(More radically, one could wonder whether view itself should behave like getindex when indexing with a scalar, and return the element rather than a view. Though that would be problematic if the goal was to mutate the parent array.)

@nalimilan nalimilan added the arrays [a, r, r, a, y, s] label Sep 18, 2017
@nalimilan nalimilan added the triage This should be discussed on a triage call label Feb 17, 2018
@nalimilan
Copy link
Member Author

Reactions about this (old) proposal?

@JeffBezanson
Copy link
Member

I think we can close this; @view and @views have different purposes. @view is just an alternate syntax for calling view, while @views is a transformation for performance.

@vtjnash vtjnash closed this as completed Feb 22, 2018
@JeffBezanson JeffBezanson removed the triage This should be discussed on a triage call label Feb 22, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
arrays [a, r, r, a, y, s]
Projects
None yet
Development

No branches or pull requests

3 participants