RFC: Add `@view` macro
This pull request adds a @view macro, which translates A[a:b, c:d] syntax to view(A, a:b, c:d).
Any symbol("end")s found in square brackets (unless they occur in nested square brackets) are converted to either size(A, d) or, if only one indexing object, length(A). The special case of 1:end is replaced by : instead of 1:size(A, d). For example:
@view(A[5:end-2])=>view(A, 5:length(A)-2)@view(A[5:end, 3:b])=>view(A, 5:size(A,1), 3:b)@view(A[1:end, 3:b])=>view(A, :, 3:b)@view(A[c[3:end]])=>view(A, :, c[3:end])
@view only works on named arrays. @view(rand(10,10)[1:5, :]) will throw an error, because I don't think you'd want a view of an array that wasn't a named object very often.
I'm making some assumptions that I'm not 100% sure of, so please let me know if they could cause an issue:
symbol("end")can only occur in an expression or subexpression of an expression with head:ref- It always makes sense to replace
symbol("end")with either a call tosizeorlength - If there is a
symbol("end")in a:refexpression that is a subexpression of another:ref, thesymbol("end")applies to the innermost:refexpression.
I also have a bunch of tests but I imagine they wouldn't be considered good practice for testing macros, so any feedback on that would be appreciated!
@lendle This is great to have!
I think more tests to ensure correctness in more cases would be good too.