Overriding show?

how can I customize the (REPL) display of built-in types? I tried:

julia> Base.show(x::Int)= if (x<10) print(x) else print("large"); end;#if#

julia> show(12)

julia> 12  ## what is actually invoked here?

This is of course a terrible example. A more useful example would switch to scientific notation for my choice of small values. Unfortunately, a couple of my attempts here segfaulted Julia:

julia> using Printf;

julia> import Base.show

julia> show(io::IO, x::Float64)= if (x<0.01) print(io, @sprintf("%e", x)) else print(io,x); end
show (generic function with 296 methods)

julia> show( 0.0001 )
julia> show( 0.01 )
Segmentation fault: 11

You left out the io::IO part of the show definition in your first exmaple. When I try it out like this

julia> Base.show(io::IO,x::Int)= if (x<10) print(x) else print("large"); end

it actually crashes Julia for me with a stack over flow error… (not a handled Julia error, but a full crash of Julia).

mine, too. which is why I wanted to start it with a non-crasher.

julia> Base.show(io::IO,x::Int)= if (x<10); print(io,"small"); else; print(io,"large"); end




In your example print(x) calls your newly defined show method and that predictable creates a stack overflow. Overloading that particular method seems to mess up the Julia REPL a bit though…

it would be best to know how to do it right, though…if it even can be done.

PS: IMHO, nothing should be able to segfault julia’s REPL.

You should’t do it so there isn’t a right way to do it.

That is not the case. Crash on 'convert(::Type{Vector{T}, v::T) where T' · Issue #29562 · JuliaLang/julia · GitHub

I clicked around on a few issue links, and there was an interesting discussion here: