Skip to content

Commit

Permalink
Print symbols in Tuple{:sym} type with colons (#42999)
Browse files Browse the repository at this point in the history
Use `show` instead of `print` or `join`.
  • Loading branch information
inkydragon authored Nov 10, 2021
1 parent 8233847 commit 6fbfc4f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
11 changes: 9 additions & 2 deletions base/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -993,10 +993,17 @@ function show_datatype(io::IO, @nospecialize(x::DataType), wheres::Vector=TypeVa
# Print homogeneous tuples with more than 3 elements compactly as NTuple{N, T}
if istuple
if n > 3 && all(@nospecialize(i) -> (parameters[1] === i), parameters)
print(io, "NTuple{", n, ", ", parameters[1], "}")
print(io, "NTuple{", n, ", ")
show(io, parameters[1])
print(io, "}")
else
print(io, "Tuple{")
join(io, parameters, ", ")
# join(io, params, ", ") params but `show` it
first = true
for param in parameters
first ? (first = false) : print(io, ", ")
show(io, param)
end
print(io, "}")
end
else
Expand Down
14 changes: 14 additions & 0 deletions test/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1347,6 +1347,20 @@ test_repr("(:).a")
@test repr(Tuple{Float64, Float64, Float64, Float64}) == "NTuple{4, Float64}"
@test repr(Tuple{Float32, Float32, Float32}) == "Tuple{Float32, Float32, Float32}"

@testset "issue #42931" begin
@test repr(NTuple{4, :A}) == "NTuple{4, :A}"
@test repr(NTuple{3, :A}) == "Tuple{:A, :A, :A}"
@test repr(NTuple{2, :A}) == "Tuple{:A, :A}"
@test repr(NTuple{1, :A}) == "Tuple{:A}"
@test repr(NTuple{0, :A}) == "Tuple{}"

@test repr(Tuple{:A, :A, :A, :B}) == "Tuple{:A, :A, :A, :B}"
@test repr(Tuple{:A, :A, :A, :A}) == "NTuple{4, :A}"
@test repr(Tuple{:A, :A, :A}) == "Tuple{:A, :A, :A}"
@test repr(Tuple{:A}) == "Tuple{:A}"
@test repr(Tuple{}) == "Tuple{}"
end

# Test that REPL/mime display of invalid UTF-8 data doesn't throw an exception:
@test isa(repr("text/plain", String(UInt8[0x00:0xff;])), String)

Expand Down

0 comments on commit 6fbfc4f

Please sign in to comment.