Skip to content

Commit

Permalink
clarify show doc strings
Browse files Browse the repository at this point in the history
fixes #36072
  • Loading branch information
JeffBezanson committed May 29, 2020
1 parent 38a373a commit 6b4ca42
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 18 deletions.
18 changes: 10 additions & 8 deletions base/multimedia.jl
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ showable(::MIME{mime}, @nospecialize x) where {mime} = hasmethod(show, Tuple{IO,
showable(m::AbstractString, @nospecialize x) = showable(MIME(m), x)

"""
show(io, mime, x)
show(io::IO, mime, x)
The [`display`](@ref) functions ultimately call `show` in order to write an object `x` as a
given `mime` type to a given I/O stream `io` (usually a memory buffer), if possible. In order
Expand All @@ -94,18 +94,20 @@ your images to be displayed on any PNG-capable `AbstractDisplay` (such as IJulia
to `import Base.show` in order to add new methods to the built-in Julia function
`show`.
The default MIME type is `MIME"text/plain"`. There is a fallback definition for `text/plain`
output that calls `show` with 2 arguments. Therefore, this case should be handled by
defining a 2-argument `show(io::IO, x::MyType)` method.
Technically, the `MIME"mime"` macro defines a singleton type for the given `mime` string,
which allows us to exploit Julia's dispatch mechanisms in determining how to display objects
of any given type.
The first argument to `show` can be an [`IOContext`](@ref) specifying output format properties.
See [`IOContext`](@ref) for details.
The default MIME type is `MIME"text/plain"`. There is a fallback definition for `text/plain`
output that calls `show` with 2 arguments, so it is not always necessary to add a method
for that case. If a type benefits from custom human-readable output though,
`show(::IO, ::MIME"text/plain", ::T)` should be defined. For example, the `Day` type uses
`1 day` as the output for the `text/plain` MIME type, and `Day(1)` as the output of 2-argument `show`.
Container types generally implement 3-argument `show` by calling `show(io, MIME"text/plain"(), x)`
for elements `x`, with `:compact => true` set in an [`IOContext`](@ref) passed as the first argument.
"""
show(stream, mime, x)
show(stream::IO, mime, x)
show(io::IO, m::AbstractString, x) = show(io, MIME(m), x)

"""
Expand Down
23 changes: 15 additions & 8 deletions base/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -265,9 +265,9 @@ the properties of that stream (note that `io` can itself be an `IOContext`).
The following properties are in common use:
- `:compact`: Boolean specifying that small values should be printed more compactly, e.g.
- `:compact`: Boolean specifying that values should be printed more compactly, e.g.
that numbers should be printed with fewer digits. This is set when printing array
elements.
elements. `:compact` output should not contain line breaks.
- `:limit`: Boolean specifying that containers should be truncated, e.g. showing `…` in
place of most elements.
- `:displaysize`: A `Tuple{Int,Int}` giving the size in rows and columns to use for text
Expand Down Expand Up @@ -358,14 +358,21 @@ function show_circular(io::IOContext, @nospecialize(x))
end

"""
show(x)
show([io::IO = stdout], x)
Write an informative text representation of a value to the current output stream. New types
should overload `show(io::IO, x)` where the first argument is a stream. The representation used
by `show` generally includes Julia-specific formatting and type information.
Write a text representation of a value `x` to the output stream `io`. New types `T`
should overload `show(io::IO, x::T)`. The representation used by `show` generally
includes Julia-specific formatting and type information, and should be parseable
Julia code when possible.
[`repr`](@ref) returns the output of `show` as a string.
To customize human-readable text output for objects of type `T`, define
`show(io::IO, ::MIME"text/plain", ::T)` instead. Checking the `:compact`
[`IOContext`](@ref) property of `io` in such methods is recommended,
since some containers show their elements by calling this method with
`:compact => true`.
See also [`print`](@ref), which writes un-decorated representations.
# Examples
Expand All @@ -376,10 +383,10 @@ julia> print("Hello World!")
Hello World!
```
"""
show(x) = show(stdout::IO, x)

show(io::IO, @nospecialize(x)) = show_default(io, x)

show(x) = show(stdout::IO, x)

# avoid inferring show_default on the type of `x`
show_default(io::IO, @nospecialize(x)) = _show_default(io, inferencebarrier(x))

Expand Down
4 changes: 2 additions & 2 deletions doc/src/base/io-network.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ Base.IOContext(::IO, ::IOContext)
## Text I/O

```@docs
Base.show(::Any)
Base.show(::IO, ::Any)
Base.summary
Base.print
Base.println
Expand Down Expand Up @@ -93,7 +93,7 @@ Base.AbstractDisplay
Base.Multimedia.display
Base.Multimedia.redisplay
Base.Multimedia.displayable
Base.show(::Any, ::Any, ::Any)
Base.show(::IO, ::Any, ::Any)
Base.Multimedia.showable
Base.repr(::MIME, ::Any)
Base.MIME
Expand Down

0 comments on commit 6b4ca42

Please sign in to comment.