What show function is called in the REPL?

I assumed it was just show but at least for arrays something else is going on:

julia> rand(3,3)
3×3 Array{Float64,2}:
 0.945084   0.86556   0.727404
 0.128891   0.180756  0.228008
 0.0498265  0.639407  0.20455 

julia> sprint(show,rand(3,3))
"[0.397804 0.757501 0.348905; 0.113848 0.0424663 0.326405; 0.368243 0.606226 0.0291582]"
julia> show(STDOUT, MIME"text/plain"(), rand(3,3))
3×3 Array{Float64,2}:
 0.753873  0.651132  0.0710547
 0.475354  0.970687  0.108144 
 0.813717  0.775238  0.681004 

See also the Custom Pretty-printing section of the manual.


Thanks, perfect.

Actually it’s not quite that, rand(100,100) in the repl prints only parts of the values with some … in between, while sprint(Base.show,MIME"text/plain"(), rand(100,100)) prints the whole thing, which gets very slow for large matrices.

1 Like

Ho, I need to use an IOContext:

    io = IOBuffer()
    io = IOContext(io,:display_size=>(20,20))
    io = IOContext(io,:limit=>true)

You can also use display, in my REPL this looks like:

julia> show(rand(2,2))
[0.305285 0.479731; 0.260545 0.235768]
julia> display(rand(2,2))
2×2 Array{Float64,2}:
 0.952205  0.839303
 0.144267  0.90555
1 Like

Sorry to revive an old thread, but I have one more question about this.

The docs agree with everything said above, that technically the REPL calls display(), but you can also use show(stdout, MIME("text/plain"), z) to achieve the same thing:

Technically, the REPL calls display(z) to display the result of executing a line, which defaults to show(stdout, MIME("text/plain"), z) , which in turn defaults to show(stdout, z) , but you should not define new display methods unless you are defining a new multimedia display handler (see Multimedia I/O).

But I was a little surprised by the difference in how nothing is handled. Both of those methods above, print nothing for display(nothing), where the REPL just doesn’t print anything:

julia> display(nothing)

julia> show(stdout, MIME("text/plain"), nothing)
julia> nothing


Is there a bit more magic where the REPL actually has an if-statement that only calls display() if the result is not a Nothing?

EDIT: Ah, yes, here it is. I managed to find it after a bit more digging; I’m pretty sure this is exactly that if-statement:

This is a bit awkward since nothing is now part of the API of important functions, e.g.:

    julia> findfirst([false])