# Call original method when redefining a method. (Compact float printing in IJulia)

**URL:** https://discourse.julialang.org/t/call-original-method-when-redefining-a-method-compact-float-printing-in-ijulia/74111
**Category:** New to Julia
**Tags:** question, repl, ijulia, show, unitful
**Created:** [January 6, 2022, 12:21am UTC](https://discourse.julialang.org/t/call-original-method-when-redefining-a-method-compact-float-printing-in-ijulia/74111 "2022-01-06T00:21:47Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![tfiers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tfiers/32/32427_2.png) [@tfiers](https://discourse.julialang.org/u/tfiers)
#### Post date: [January 6, 2022, 12:21am UTC](https://discourse.julialang.org/t/call-original-method-when-redefining-a-method-compact-float-printing-in-ijulia/74111/1 "2022-01-06T00:21:47Z")

</div>

I want to print floats (and derived types such as `Unitful.Quantity{Float64, …}`) compactly in IJulia / the REPL.

E.g. `0.1*3` should be echoed as `0.3` and not `0.30000000000000004`.  
Likewise, `0.1*3mV` should be `0.3 mV` and not `0.30000000000000004 mV`.

The canonical way to print compactly seems to be `show(IOContext(stdout, :compact=>true), 0.1*3)`.

To achieve this automatically for all echoes (i.e. without having to type the above `show` incantation in every cell), I’d like to define something like:

```julia
Base.show(io::IO, x::Float64) = 
    show(IOContext(io, :compact=>true), x)

```

This yields a StackOverflowError because we redefined the method in terms of itself.  
The desired behaviour would be for the `show` in our function’s body to refer to [the original float-showing method in Base](https://github.com/JuliaLang/julia/blob/3bf9d1773144bc4943232dc2ffaac307a700853d/base/ryu/Ryu.jl?_pjax=%23repo-content-pjax-container#L111).

My question is, how can we do this?  
More generally: how do you call the original method when redefining a method?

(Research note: in [a previous thread](https://discourse.julialang.org/t/compact-float-printing-in-the-repl/4561/5) on the same problem this was resolved by calling `Base.Grisu._show`. This cannot be done anymore as “Grisu” has been replaced by “Ryu”, and Ryu only defines `Base.show` itself, not any inner function like `_show`.)

---

<div class="post-metadata">

### Author: ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)
#### Post date: [January 6, 2022, 12:38am UTC](https://discourse.julialang.org/t/call-original-method-when-redefining-a-method-compact-float-printing-in-ijulia/74111/2 "2022-01-06T00:38:15Z")

</div>

Maybe you can take a look at

[https://github.com/ronisbr/PrettyNumbers.jl](https://github.com/ronisbr/PrettyNumbers.jl)

---

<div class="post-metadata">

### Author: ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)
#### Post date: [January 6, 2022, 12:42am UTC](https://discourse.julialang.org/t/call-original-method-when-redefining-a-method-compact-float-printing-in-ijulia/74111/3 "2022-01-06T00:42:04Z")

</div>

To call the old method, you want `invoke`, or now `@invoke`:

```julia
julia> @which show(stdout, 3.14)
show(io::IO, x::T) where T<:Union{Float16, Float32, Float64} in Base.Ryu at ryu/Ryu.jl:111

julia> Base.show(io::IO, x::Float64) = 
             Base.@invoke show(IOContext(io, :compact=>true)::IO, x::Union{Float16, Float32, Float64})

julia> pi .^ (0,1,2)
(1.0, 3.14159, 9.8696)

```

---

<div class="post-metadata">

### Author: ![tfiers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tfiers/32/32427_2.png) [@tfiers](https://discourse.julialang.org/u/tfiers)
#### Post date: [January 6, 2022, 3:16am UTC](https://discourse.julialang.org/t/call-original-method-when-redefining-a-method-compact-float-printing-in-ijulia/74111/4 "2022-01-06T03:16:41Z")

</div>

Thank you, that works nicely.  
TIL about `invoke`.

This does not seem to work if the overriding method has the exact same signature though.

For example, I want to override `show(io::IO, x::Quantity)` [from Unitful](https://github.com/PainterQubits/Unitful.jl/blob/75f222be21d851970cc178d62110c22bef0ac4ad/src/display.jl#L109-L113) and use the original function definition in my new method. I currently just copy-paste the original function body (and import needed symbols), but that’s not super nice.

---

<div class="post-metadata">

### Author: ![tfiers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tfiers/32/32427_2.png) [@tfiers](https://discourse.julialang.org/u/tfiers)
#### Post date: [February 8, 2022, 1:39pm UTC](https://discourse.julialang.org/t/call-original-method-when-redefining-a-method-compact-float-printing-in-ijulia/74111/5 "2022-02-08T13:39:16Z")

</div>

> [@tfiers](#):
>
> This does not seem to work if the overriding method has the exact same signature though.

For that, use `invoke`, not `@invoke`.

---

<div class="post-metadata">

### Author: ![Ouroboros42](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ouroboros42/32/216175_2.png) [@Ouroboros42](https://discourse.julialang.org/u/Ouroboros42)
#### Post date: [April 6, 2025, 1:51am UTC](https://discourse.julialang.org/t/call-original-method-when-redefining-a-method-compact-float-printing-in-ijulia/74111/6 "2025-04-06T01:51:46Z")

</div>

Would you be able to explain how you did that? (Invoke old method with identical signature).

Just calling `invoke` in the regular way causes recursion for me.

---

<div class="post-metadata">

### Author: ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)
#### Post date: [April 6, 2025, 5:24am UTC](https://discourse.julialang.org/t/call-original-method-when-redefining-a-method-compact-float-printing-in-ijulia/74111/7 "2025-04-06T05:24:25Z")

</div>

Welcome to the discourse, I suggest starting your own post for your problem instead of trying to continue a thread that was last active over 3 years ago. A solved thread won’t be promoted as much in feeds, and the participants in this thread are far less likely to return after so long to interact with you. With very few exceptions like developers updating package announcement threads, it’s very typical to start a new thread after a month. If you like, you could link older threads in your post, but that may not be necessary because your minimum working example would usually demonstrate your issue adequately.
