# Overriding show?

**URL:** https://discourse.julialang.org/t/overriding-show/16315
**Category:** New to Julia
**Created:** [October 14, 2018, 6:31pm UTC](https://discourse.julialang.org/t/overriding-show/16315 "2018-10-14T18:31:00Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![iwelch](https://avatars.discourse-cdn.com/v4/letter/i/8c91f0/32.png) [@iwelch](https://discourse.julialang.org/u/iwelch)
#### Post date: [October 14, 2018, 6:31pm UTC](https://discourse.julialang.org/t/overriding-show/16315/1 "2018-10-14T18:31:00Z")

</div>

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

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

julia> show(12)
large

julia> 12 ## what is actually invoked here?
12

```

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
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 )
1.000000e-04
julia> show( 0.01 )
Segmentation fault: 11

```

---

<div class="post-metadata">

### Author: ![chakravala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chakravala/32/6832_2.png) [@chakravala](https://discourse.julialang.org/u/chakravala)
#### Post date: [October 14, 2018, 6:56pm UTC](https://discourse.julialang.org/t/overriding-show/16315/2 "2018-10-14T18:56:30Z")

</div>

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

```nohighlight
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).

---

<div class="post-metadata">

### Author: ![iwelch](https://avatars.discourse-cdn.com/v4/letter/i/8c91f0/32.png) [@iwelch](https://discourse.julialang.org/u/iwelch)
#### Post date: [October 14, 2018, 7:00pm UTC](https://discourse.julialang.org/t/overriding-show/16315/3 "2018-10-14T19:00:10Z")

</div>

> [@chakravala](#):
>
> ```julia
> julia> Base.show(io::IO,x::Int)= if (x<10) print(x) else print("large"); end
> 
> ```

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

---

<div class="post-metadata">

### Author: ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)
#### Post date: [October 14, 2018, 7:15pm UTC](https://discourse.julialang.org/t/overriding-show/16315/4 "2018-10-14T19:15:44Z")

</div>

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

mallC>

mallC21
large

mallC1
small

```

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…

---

<div class="post-metadata">

### Author: ![iwelch](https://avatars.discourse-cdn.com/v4/letter/i/8c91f0/32.png) [@iwelch](https://discourse.julialang.org/u/iwelch)
#### Post date: [October 14, 2018, 8:21pm UTC](https://discourse.julialang.org/t/overriding-show/16315/5 "2018-10-14T20:21:51Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)
#### Post date: [October 14, 2018, 8:30pm UTC](https://discourse.julialang.org/t/overriding-show/16315/6 "2018-10-14T20:30:53Z")

</div>

> [@iwelch](#):
>
> it would be best to know how to do it right, though…if it even can be done.

> [@iwelch](#):
>
> customize the (REPL) display of built-in types

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

> [@iwelch](#):
>
> IMHO, nothing should be able to segfault julia’s REPL.

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

---

<div class="post-metadata">

### Author: ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)
#### Post date: [October 14, 2018, 8:42pm UTC](https://discourse.julialang.org/t/overriding-show/16315/7 "2018-10-14T20:42:28Z")

</div>

I clicked around on a few issue links, and there was an interesting discussion here:  
[https://github.com/JuliaLang/julia/issues/20945](https://github.com/JuliaLang/julia/issues/20945)
