diff --git a/src/array/indexing.jl b/src/array/indexing.jl index a0dca3213..540e4ec12 100644 --- a/src/array/indexing.jl +++ b/src/array/indexing.jl @@ -82,3 +82,6 @@ end setindex!(A, x, dims2indices(A, (i, I...))...) @propagate_inbounds Base.setindex!(A::AbstractDimArray, x, i1::StandardIndices, I::StandardIndices...) = setindex!(parent(A), x, i1, I...) + +# For @views macro to work with keywords +Base.maybeview(A::AbstractDimArray, args...; kw...) = view(A, args...; kw...) diff --git a/src/stack/indexing.jl b/src/stack/indexing.jl index 8b9edb70d..cf62404b2 100644 --- a/src/stack/indexing.jl +++ b/src/stack/indexing.jl @@ -38,3 +38,6 @@ end end @noinline _keysmismatch(K1, K2) = throw(ArgumentError("NamedTuple keys $K2 do not mach stack keys $K1")) + +# For @views macro to work with keywords +Base.maybeview(A::AbstractDimStack, args...; kw...) = view(A, args...; kw...) diff --git a/test/indexing.jl b/test/indexing.jl index 5802fa5ba..ac8b98856 100644 --- a/test/indexing.jl +++ b/test/indexing.jl @@ -235,6 +235,12 @@ end @test length(refdims(a)) == 2 end end + + @testset "@views macro and maybeview work even with kw syntax" begin + v1 = @views da[Y(1:2), X(1)] + v2 = @views da[Y=1:2, X=1] + @test v1 == v2 == [1, 2] + end end @testset "setindex!" begin @@ -395,6 +401,11 @@ end @test view(s, X(1), Y(1))[:two] == view(da2, X(1), Y(1)) @test view(s, X(2), Y(3))[:three] == view(da3, X(2), Y(3)) end + @testset "@views macro and maybeview work even with kw syntax" begin + sv1 = @views s[X(1:2), Y(3:3)] + sv2 = @views s[X=1:2, Y=3:3] + @test parent(sv1) == parent(sv2) == (one=[3.0 6.0]', two=[6.0f0 12.0f0]', three=[9 18]') + end end @testset "setindex!" begin