Skip to content

Commit 6fbfc4f

Browse files
authored
Print symbols in Tuple{:sym} type with colons (#42999)
Use `show` instead of `print` or `join`.
1 parent 8233847 commit 6fbfc4f

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

base/show.jl

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -993,10 +993,17 @@ function show_datatype(io::IO, @nospecialize(x::DataType), wheres::Vector=TypeVa
993993
# Print homogeneous tuples with more than 3 elements compactly as NTuple{N, T}
994994
if istuple
995995
if n > 3 && all(@nospecialize(i) -> (parameters[1] === i), parameters)
996-
print(io, "NTuple{", n, ", ", parameters[1], "}")
996+
print(io, "NTuple{", n, ", ")
997+
show(io, parameters[1])
998+
print(io, "}")
997999
else
9981000
print(io, "Tuple{")
999-
join(io, parameters, ", ")
1001+
# join(io, params, ", ") params but `show` it
1002+
first = true
1003+
for param in parameters
1004+
first ? (first = false) : print(io, ", ")
1005+
show(io, param)
1006+
end
10001007
print(io, "}")
10011008
end
10021009
else

test/show.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1347,6 +1347,20 @@ test_repr("(:).a")
13471347
@test repr(Tuple{Float64, Float64, Float64, Float64}) == "NTuple{4, Float64}"
13481348
@test repr(Tuple{Float32, Float32, Float32}) == "Tuple{Float32, Float32, Float32}"
13491349

1350+
@testset "issue #42931" begin
1351+
@test repr(NTuple{4, :A}) == "NTuple{4, :A}"
1352+
@test repr(NTuple{3, :A}) == "Tuple{:A, :A, :A}"
1353+
@test repr(NTuple{2, :A}) == "Tuple{:A, :A}"
1354+
@test repr(NTuple{1, :A}) == "Tuple{:A}"
1355+
@test repr(NTuple{0, :A}) == "Tuple{}"
1356+
1357+
@test repr(Tuple{:A, :A, :A, :B}) == "Tuple{:A, :A, :A, :B}"
1358+
@test repr(Tuple{:A, :A, :A, :A}) == "NTuple{4, :A}"
1359+
@test repr(Tuple{:A, :A, :A}) == "Tuple{:A, :A, :A}"
1360+
@test repr(Tuple{:A}) == "Tuple{:A}"
1361+
@test repr(Tuple{}) == "Tuple{}"
1362+
end
1363+
13501364
# Test that REPL/mime display of invalid UTF-8 data doesn't throw an exception:
13511365
@test isa(repr("text/plain", String(UInt8[0x00:0xff;])), String)
13521366

0 commit comments

Comments
 (0)