Skip to content

Commit 4b8fde3

Browse files
stevengjfingolfin
andauthored
add summary of x::T output functions (#54547)
This adds a brief summary of the different output functions (`display`, `show`, `print`, `repr`, etc.) and how they interact to the custom pretty-printing section of the manual, following [this post on discourse](https://discourse.julialang.org/t/how-to-display-a-sparse-array-on-the-repl/113565/5), since people often get confused by this. --------- Co-authored-by: Max Horn <max@quendi.de>
1 parent 88d683c commit 4b8fde3

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

doc/src/manual/types.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1587,6 +1587,19 @@ julia> [Polar(3, 4.0) Polar(4.0,5.3)]
15871587
See the [`IOContext`](@ref) documentation for a list of common properties which can be used
15881588
to adjust printing.
15891589

1590+
### Output-function summary
1591+
1592+
Here is a brief summary of the different output functions in Julia and how they are related.
1593+
Most new types should only need to define `show` methods, if anything.
1594+
1595+
* [`display(x)`](@ref) tells the current environment to display `x` in whatever way it thinks best. (This might even be a graphical display in something like a Jupyter or Pluto notebook.) By default (e.g. in scripts or in the text REPL), it calls `show(io, "text/plain", x)`, or equivalently `show(io, MIME"text/plain"(), x)`, for an appropriate `io` stream. (In the REPL, `io` is an [`IOContext`](@ref) wrapper around [`stdout`](@ref).) The REPL uses `display` to output the result of an evaluated expression.
1596+
* The 3-argument [`show(io, ::MIME"text/plain", x)`](@ref) method performs verbose pretty-printing of `x`. By default (if no 3-argument method is defined for `typeof(x)`), it calls the 2-argument `show(io, x)`. It is called by the 2-argument `repr("text/plain", x)`. Other 3-argument `show` methods can be defined for additional MIME types as discussed above, to enable richer display of `x` in some interactive environments.
1597+
* The 2-argument [`show(io, x)`](@ref) is the default simple text representation of `x`. It is called by the 1-argument [`repr(x)`](@ref), and is typically the format you might employ to input `x` into Julia. The 1-argument `show(x)` calls `show(stdout, x)`.
1598+
* [`print(io, x)`](@ref) by default calls `show(io, x)`, but a few types have a distinct `print` format — most notably, when `x` is a string, `print` outputs the raw text whereas `show` outputs an escaped string enclosed in quotation marks. The 1-argument `print(x)` calls `print(stdout, x)`. `print` is also called by [`string(x)`](@ref).
1599+
* [`write(io, x)`](@ref), if it is defined (it generally has *no* default definition for new types), writes a "raw" binary representation of `x` to `io`, e.g. an `x::Int32` will be written as 4 bytes.
1600+
1601+
It is also helpful to be familiar with the metadata that can be attached to an `io` stream by an [`IOContext`](@ref) wrapper. For example, the REPL sets the `:limit => true` flag from `display` for an evaluated expression, in order to limit the output to fit in the terminal; you can query this flag with `get(io, :limit, false)`. And when displaying an object contained within, for example, a multi-column matrix, the `:compact => true` flag could be set, which you can query with `get(io, :compact, false)`.
1602+
15901603
## "Value types"
15911604

15921605
In Julia, you can't dispatch on a *value* such as `true` or `false`. However, you can dispatch

0 commit comments

Comments
 (0)