FYI, following IOContext
are provided to show
for the case the object in question is in a container (see below for the code to check it):
|
:typeinfo |
:limit |
:compact |
:color |
display([T()]) |
T |
true |
|
true |
show([T()]) |
T |
|
true |
true |
repr([T()]) |
T |
|
true |
|
display(Dict(:a=>T())) |
|
true |
true |
true |
show(Dict(:a=>T())) |
T |
|
true |
true |
repr(Dict(:a=>T())) |
T |
|
true |
|
From this, it seems that differentiating display
(i.e., REPL) and repr
can be done by simply looking at :limit
in show(::IO, ::T)
. See also: I/O and Network · The Julia Language
I also add that the Style Guide seems to explicitly discourage AbstractArray{<:Cyc}
solution:
Don’t overload methods of base container types
It is possible to write definitions like the following:
show(io::IO, v::Vector{MyType}) = ...
This would provide custom showing of vectors with a specific new element type. While tempting, this should be avoided. The trouble is that users will expect a well-known type like Vector() to behave in a certain way, and overly customizing its behavior can make it harder to work with.
— Style Guide · The Julia Language
…although it does not quite apply to your case, since you just use the container’s show
almost as-is.
Aren’t you just need to check if repr
or not, rather than REPL-or-not? If so, I think an alternative approach would be to add a public API show(::IO, ::MIME"application/julia", ::MyType)
which is called from repr
if it exists. (I know this MIME is not super legit but julia
actually already “knows” it.)
Code to check the table
julia> struct LogIO end
julia> logio_log = []
0-element Array{Any,1}
julia> function logio_show(io, mime)
push!(logio_log, ("mime", mime))
push!(logio_log, ("io isa IOContext", io isa IOContext))
if io isa IOContext
push!(logio_log, ("io.dict", io.dict))
end
print(io, "LogIO")
end
logio_show (generic function with 1 method)
julia> Base.show(io::IO, mime::MIME"text/plain", ::LogIO) =
logio_show(io, mime)
julia> Base.show(io::IO, ::LogIO) =
logio_show(io, nothing)
julia> empty!(logio_log)
display([LogIO()])
logio_log
1-element Array{LogIO,1}:
LogIO
9-element Array{Any,1}:
("mime", nothing)
("io isa IOContext", true)
("io.dict", Base.ImmutableDict{Symbol,Any}(:typeinfo=>LogIO,:module=>Main,:limit=>true,:color=>true))
("mime", nothing)
("io isa IOContext", true)
("io.dict", Base.ImmutableDict{Symbol,Any}(:typeinfo=>LogIO,:module=>Main,:limit=>true,:color=>true))
("mime", nothing)
("io isa IOContext", true)
("io.dict", Base.ImmutableDict{Symbol,Any}(:typeinfo=>LogIO,:module=>Main,:limit=>true,:color=>true))
julia> empty!(logio_log)
show([LogIO()])
logio_log
LogIO[LogIO]3-element Array{Any,1}:
("mime", nothing)
("io isa IOContext", true)
("io.dict", Base.ImmutableDict{Symbol,Any}(:SHOWN_SET=>LogIO[LogIO],:compact=>true,:typeinfo=>LogIO,:color=>true))
julia> empty!(logio_log)
repr([LogIO()])
logio_log
3-element Array{Any,1}:
("mime", nothing)
("io isa IOContext", true)
("io.dict", Base.ImmutableDict{Symbol,Any}(:SHOWN_SET=>LogIO[LogIO],:compact=>true,:typeinfo=>LogIO))
julia> empty!(logio_log)
display(Dict(:a=>LogIO()))
logio_log
Dict{Symbol,LogIO} with 1 entry:
:a => LogIO
3-element Array{Any,1}:
("mime", nothing)
("io isa IOContext", true)
("io.dict", Base.ImmutableDict{Symbol,Any}(:compact=>true,:SHOWN_SET=>Dict(:a=>LogIO),:module=>Main,:limit=>true,:color=>true))
julia> empty!(logio_log)
show(Dict(:a=>LogIO()))
logio_log
Dict(:a=>LogIO)3-element Array{Any,1}:
("mime", nothing)
("io isa IOContext", true)
("io.dict", Base.ImmutableDict{Symbol,Any}(:typeinfo=>LogIO,:compact=>true,:compact=>true,:typeinfo=>Pair{Symbol,LogIO},:SHOWN_SET=>Dict(:a=>LogIO),:color=>true))
julia> empty!(logio_log)
repr(Dict(:a=>LogIO()))
logio_log
3-element Array{Any,1}:
("mime", nothing)
("io isa IOContext", true)
("io.dict", Base.ImmutableDict{Symbol,Any}(:typeinfo=>LogIO,:compact=>true,:compact=>true,:typeinfo=>Pair{Symbol,LogIO},:SHOWN_SET=>Dict(:a=>LogIO)))