From 79f31e44087425e552571b6f94da364830b5c113 Mon Sep 17 00:00:00 2001 From: Curtis Vogt Date: Thu, 13 Feb 2020 09:14:10 -0600 Subject: [PATCH] Add compact 3-arg show for Irrational (#34741) --- base/irrationals.jl | 6 +++++- test/numbers.jl | 6 ++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/base/irrationals.jl b/base/irrationals.jl index b147959667abf..4b42aa9c05a26 100644 --- a/base/irrationals.jl +++ b/base/irrationals.jl @@ -20,7 +20,11 @@ struct Irrational{sym} <: AbstractIrrational end show(io::IO, x::Irrational{sym}) where {sym} = print(io, sym) function show(io::IO, ::MIME"text/plain", x::Irrational{sym}) where {sym} - print(io, sym, " = ", string(float(x))[1:15], "...") + if get(io, :compact, false) + print(io, sym) + else + print(io, sym, " = ", string(float(x))[1:15], "...") + end end promote_rule(::Type{<:AbstractIrrational}, ::Type{Float16}) = Float16 diff --git a/test/numbers.jl b/test/numbers.jl index d03a52fa8f12a..31761880f84c0 100644 --- a/test/numbers.jl +++ b/test/numbers.jl @@ -1078,6 +1078,12 @@ end @test sqrt(2) == 1.4142135623730951 end +@testset "Irrational printing" begin + @test sprint(show, "text/plain", π) == "π = 3.1415926535897..." + @test sprint(show, "text/plain", π, context=:compact => true) == "π" + @test sprint(show, π) == "π" + +end @testset "issue #6365" begin for T in (Float32, Float64) for i = 9007199254740992:9007199254740996