Skip to content

Commit

Permalink
Allow CartesianIndices with Bool argument (JuliaLang#39962)
Browse files Browse the repository at this point in the history
Follow up to the problem discussed in JuliaLang#31829 (comment).

I came to the conclusion that `CartesianIndices((true,))` should be allowed as in this context `true` represents a dimension length not an index.
  • Loading branch information
bkamins authored and ElOceanografo committed May 4, 2021
1 parent d7c3280 commit 6e02d5d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
1 change: 1 addition & 0 deletions base/multidimensional.jl
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ module IteratorsMD

CartesianIndices(A::AbstractArray) = CartesianIndices(axes(A))

_convert2ind(sz::Bool) = Base.OneTo(Int(sz))
_convert2ind(sz::Integer) = Base.OneTo(sz)
_convert2ind(sz::AbstractUnitRange) = first(sz):last(sz)
_convert2ind(sz::OrdinalRange) = first(sz):step(sz):last(sz)
Expand Down
7 changes: 7 additions & 0 deletions test/cartesian.jl
Original file line number Diff line number Diff line change
Expand Up @@ -409,3 +409,10 @@ end
# issue #39705
f39705() = Base.Cartesian.@nany 0 _ -> true
@test f39705() === false

@testset "CartesianIndices with Bool" begin
@test @inferred(CartesianIndices((true,))) == CartesianIndices((1,))
@test @inferred(CartesianIndices((false,))) == CartesianIndices((0,))
@test @inferred(CartesianIndices((true, false))) == CartesianIndices((1, 0))
@test @inferred(CartesianIndices((false, true))) == CartesianIndices((0, 1))
end

0 comments on commit 6e02d5d

Please sign in to comment.