I've always been confused by this...
In https://docs.julialang.org/en/v1.6/manual/types/#man-custom-pretty-printing, this function to pretty-print polar numbers is defined:
Base.show(io::IO, ::MIME"text/html", z::Polar{T}) where {T} =
println(io, "<code>Polar{$T}</code> complex number: ",
z.r, " <i>e</i><sup>", z.Θ, " <i>i</i></sup>")
and then it is called as
show(stdout, "text/html", Polar(3.0,4.0))
Why does "text/html"
, which is a String
, match the signature ::MIME"text/html"
?
If I try to do the same in my own function, it doesn't work:
julia> ttt(::MIME"text/html") = println("yes")
julia> ttt("text/html")
ERROR: MethodError: no method matching ttt(::String)
Closest candidates are:
ttt(::MIME{Symbol("text/html")}) at REPL[14]:1
Stacktrace:
[1] top-level scope
@ REPL[16]:1
Nevermind, I found it in multimedia.jl
:
show(io::IO, m::AbstractString, x) = show(io, MIME(m), x)
That part of the manual sure is confusing.
Yeah. IMO the way we handle show
has a lot of problems. It's really useful because it gives tonnes of different ways to customize show
, but it sucks because it's really confusing and filled with edge cases.
Yes; things like the two-argument and three-argument show
variants, the reliance on MIME, plus display
, plus all the multimedia I/O stuff are confusing. I spent hours getting Gaton's show
to work properly in Pluto and IJulia, and I'm still not sure my implementation is correct.
I submitted an issue regarding show()
for containers, got no response so far... https://github.com/JuliaLang/julia/issues/40030
Last updated: Nov 22 2024 at 04:41 UTC