Skip to content

Commit 257c051

Browse files
committed
clarify show doc strings
fixes #36072
1 parent 38a373a commit 257c051

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

base/multimedia.jl

+8-6
Original file line numberDiff line numberDiff line change
@@ -94,16 +94,18 @@ your images to be displayed on any PNG-capable `AbstractDisplay` (such as IJulia
9494
to `import Base.show` in order to add new methods to the built-in Julia function
9595
`show`.
9696
97-
The default MIME type is `MIME"text/plain"`. There is a fallback definition for `text/plain`
98-
output that calls `show` with 2 arguments. Therefore, this case should be handled by
99-
defining a 2-argument `show(io::IO, x::MyType)` method.
100-
10197
Technically, the `MIME"mime"` macro defines a singleton type for the given `mime` string,
10298
which allows us to exploit Julia's dispatch mechanisms in determining how to display objects
10399
of any given type.
104100
105-
The first argument to `show` can be an [`IOContext`](@ref) specifying output format properties.
106-
See [`IOContext`](@ref) for details.
101+
The default MIME type is `MIME"text/plain"`. There is a fallback definition for `text/plain`
102+
output that calls `show` with 2 arguments, so it is not always necessary to add a method
103+
for that case. If a type benefits from custom human-readable output though,
104+
`show(::IO, ::MIME"text/plain", ::T)` should be defined. For example, the `Day` type uses
105+
`1 day` as the output for the `text/plain` MIME type, and `Day(1)` as the output of 2-argument `show`.
106+
107+
Container types should implement 3-argument `show` by calling `show(io, MIME"text/plain"(), x)`
108+
for elements `x`, with `:compact => true` set in an [`IOContext`](@ref) passed as the first argument.
107109
"""
108110
show(stream, mime, x)
109111
show(io::IO, m::AbstractString, x) = show(io, MIME(m), x)

base/show.jl

+12-6
Original file line numberDiff line numberDiff line change
@@ -358,14 +358,20 @@ function show_circular(io::IOContext, @nospecialize(x))
358358
end
359359

360360
"""
361-
show(x)
361+
show([io::IO = stdout], x)
362362
363-
Write an informative text representation of a value to the current output stream. New types
364-
should overload `show(io::IO, x)` where the first argument is a stream. The representation used
365-
by `show` generally includes Julia-specific formatting and type information.
363+
Write a text representation of a value `x` to the output stream `io`. New types `T`
364+
should overload `show(io::IO, x::T)`. The representation used
365+
by `show` generally includes Julia-specific formatting and type information, and should
366+
be parseable Julia code when possible.
366367
367368
[`repr`](@ref) returns the output of `show` as a string.
368369
370+
To customize human-readable text output for objects of type `T`, define
371+
`show(io::IO, ::MIME"text/plain", ::T)` instead. To customize how objects
372+
are shown inside containers, check the `:compact` [`IOContext`](@ref)
373+
property of `io`.
374+
369375
See also [`print`](@ref), which writes un-decorated representations.
370376
371377
# Examples
@@ -376,10 +382,10 @@ julia> print("Hello World!")
376382
Hello World!
377383
```
378384
"""
379-
show(x) = show(stdout::IO, x)
380-
381385
show(io::IO, @nospecialize(x)) = show_default(io, x)
382386

387+
show(x) = show(stdout::IO, x)
388+
383389
# avoid inferring show_default on the type of `x`
384390
show_default(io::IO, @nospecialize(x)) = _show_default(io, inferencebarrier(x))
385391

0 commit comments

Comments
 (0)