Skip to content

Commit

Permalink
Show Bools as 0 and 1 when typeinfo is a Union of Bool with Nothing o…
Browse files Browse the repository at this point in the history
…r Missing
  • Loading branch information
Lilith Hafner authored and Lilith Hafner committed Feb 28, 2023
1 parent a23b29c commit 1c482e8
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
5 changes: 4 additions & 1 deletion base/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1141,10 +1141,13 @@ function show(io::IO, tn::Core.TypeName)
end

show(io::IO, ::Nothing) = print(io, "nothing")
show(io::IO, b::Bool) = print(io, get(io, :typeinfo, Any) === Bool ? (b ? "1" : "0") : (b ? "true" : "false"))
show(io::IO, n::Signed) = (write(io, string(n)); nothing)
show(io::IO, n::Unsigned) = print(io, "0x", string(n, pad = sizeof(n)<<1, base = 16))
print(io::IO, n::Unsigned) = print(io, string(n))
function show(io::IO, b::Bool)
typeinfo = nonmissingtype(nonnothingtype(get(io, :typeinfo, Any)))
print(io, typeinfo === Bool ? (b ? "1" : "0") : (b ? "true" : "false"))
end

show(io::IO, p::Ptr) = print(io, typeof(p), " @0x$(string(UInt(p), base = 16, pad = Sys.WORD_SIZE>>2))")

Expand Down
5 changes: 5 additions & 0 deletions test/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2118,6 +2118,11 @@ replstrcolor(x) = sprint((io, x) -> show(IOContext(io, :limit => true, :color =>
@test_repr "Bool[1, 0]"
end

@testset "Unions with Bool (#39590)" begin
@test repr([missing, false]) == "Union{Missing, Bool}[missing, 0]"
@test_repr "Union{Bool, Nothing}[1, 0, nothing]"
end

# issue #30505
@test repr(Union{Tuple{Char}, Tuple{Char, Char}}[('a','b')]) == "Union{Tuple{Char}, Tuple{Char, Char}}[('a', 'b')]"

Expand Down

0 comments on commit 1c482e8

Please sign in to comment.