Skip to content

Commit 6e02d5d

Browse files
bkaminsElOceanografo
authored andcommitted
Allow CartesianIndices with Bool argument (JuliaLang#39962)
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.
1 parent d7c3280 commit 6e02d5d

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

base/multidimensional.jl

+1
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,7 @@ module IteratorsMD
278278

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

281+
_convert2ind(sz::Bool) = Base.OneTo(Int(sz))
281282
_convert2ind(sz::Integer) = Base.OneTo(sz)
282283
_convert2ind(sz::AbstractUnitRange) = first(sz):last(sz)
283284
_convert2ind(sz::OrdinalRange) = first(sz):step(sz):last(sz)

test/cartesian.jl

+7
Original file line numberDiff line numberDiff line change
@@ -409,3 +409,10 @@ end
409409
# issue #39705
410410
f39705() = Base.Cartesian.@nany 0 _ -> true
411411
@test f39705() === false
412+
413+
@testset "CartesianIndices with Bool" begin
414+
@test @inferred(CartesianIndices((true,))) == CartesianIndices((1,))
415+
@test @inferred(CartesianIndices((false,))) == CartesianIndices((0,))
416+
@test @inferred(CartesianIndices((true, false))) == CartesianIndices((1, 0))
417+
@test @inferred(CartesianIndices((false, true))) == CartesianIndices((0, 1))
418+
end

0 commit comments

Comments
 (0)