-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Open
Labels
display and printingAesthetics and correctness of printed representations of objects.Aesthetics and correctness of printed representations of objects.error handlingHandling of exceptions by Julia or the userHandling of exceptions by Julia or the user
Description
The following example was noted on discourse. Suppose we define an ambiguous Base.show method for a new type because we forget to qualify the type of the io argument:
struct Foo; x; end
Base.show(io, x::Foo) = print(io, "FOO($(x.x))") # AMBIGUOUS io argumentCalling show directly correctly gives us a method-ambiguity error:
julia> show(Foo(3))
ERROR: MethodError: show(::Base.TTY, ::Foo) is ambiguous.
Candidates:
show(io::IO, x)
@ Base show.jl:457
show(io, x::Foo)
@ Main REPL[2]:1but calling display gives us a very confusing error:
julia> display(Foo(3))
ERROR: MethodError: no method matching display(::Foo)
Closest candidates are:
display(::Any)
@ Base multimedia.jl:336
display(::TextDisplay, ::MIME{Symbol("text/plain")}, ::Any)
@ Base multimedia.jl:254
display(::TextDisplay, ::Any)
@ Base multimedia.jl:255
...
Stacktrace:
[1] display(x::Any)
@ Base.Multimedia ./multimedia.jl:347
[2] top-level scope
@ REPL[5]:1probably because of the try-catch blocks inside display that try to figure out how to display an object.
Since text/plain output should always be available as a fallback, probably the display function should not try to catch errors from this. In particular, maybe
Line 347 in e89c21b
| throw(MethodError(display, (x,))) |
should be replaced by
show(stdout, MIME"text/plain"(), x) # fallback display
which has the added benefit that display(x) will now work even if the displays stack is empty.
BeastyBlacksmith and mateuszbaran
Metadata
Metadata
Assignees
Labels
display and printingAesthetics and correctness of printed representations of objects.Aesthetics and correctness of printed representations of objects.error handlingHandling of exceptions by Julia or the userHandling of exceptions by Julia or the user