Skip to content

confusing error thrown by display #54551

@stevengj

Description

@stevengj

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 argument

Calling 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]:1

but 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]:1

probably 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

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    display and printingAesthetics and correctness of printed representations of objects.error handlingHandling of exceptions by Julia or the user

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions