Stream: helpdesk (published)

Topic: Confusion with MIME types as function arguments


view this post on Zulip mbaz (Mar 30 2021 at 17:56):

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

view this post on Zulip mbaz (Mar 30 2021 at 18:18):

Nevermind, I found it in multimedia.jl:
show(io::IO, m::AbstractString, x) = show(io, MIME(m), x)

view this post on Zulip mbaz (Mar 30 2021 at 18:19):

That part of the manual sure is confusing.

view this post on Zulip Mason Protter (Mar 31 2021 at 00:47):

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.

view this post on Zulip mbaz (Mar 31 2021 at 01:17):

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.

view this post on Zulip Kirill Simonov (Mar 31 2021 at 20:43):

I submitted an issue regarding show() for containers, got no response so far... https://github.com/JuliaLang/julia/issues/40030


Last updated: Dec 28 2024 at 04:38 UTC