Skip to content

Commit 22805e6

Browse files
authored
Skip trailing comma in displaying a 1D CartesianIndex (#57709)
On nightly v"1.13.0-DEV.186", ```julia julia> CartesianIndex(3) CartesianIndex(3,) ``` This PR ```julia julia> CartesianIndex(3) CartesianIndex(3) ``` The trailing comma is removed in the displayed form.
1 parent 3e57a8a commit 22805e6

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

base/multidimensional.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,11 @@ module IteratorsMD
9191
flatten(I::Tuple{Any}) = Tuple(I[1])
9292
@inline flatten(I::Tuple) = (Tuple(I[1])..., flatten(tail(I))...)
9393
CartesianIndex(index::Tuple{Vararg{Union{Integer, CartesianIndex}}}) = CartesianIndex(index...)
94-
show(io::IO, i::CartesianIndex) = (print(io, "CartesianIndex"); show(io, i.I))
94+
function show(io::IO, i::CartesianIndex)
95+
print(io, "CartesianIndex(")
96+
join(io, i.I, ", ")
97+
print(io, ")")
98+
end
9599

96100
# length
97101
length(::CartesianIndex{N}) where {N} = N

test/cartesian.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,3 +573,12 @@ end
573573
end
574574
@test t3 == (1, 2, 0)
575575
end
576+
577+
@testset "CartesianIndex show" begin
578+
c = CartesianIndex()
579+
@test sprint(show, c) == "CartesianIndex()"
580+
c = CartesianIndex(3)
581+
@test sprint(show, c) == "CartesianIndex(3)"
582+
c = CartesianIndex(3, 3)
583+
@test sprint(show, c) == "CartesianIndex(3, 3)"
584+
end

0 commit comments

Comments
 (0)