# How to call the original function when overriding it

**URL:** <https://discourse.julialang.org/t/how-to-call-the-original-function-when-overriding-it/56865>\
**Category:** General Usage\
**Tags:** question, function, eval\
**Created:** [March 10, 2021, 9:52am UTC](https://discourse.julialang.org/t/how-to-call-the-original-function-when-overriding-it/56865 "2021-03-10T09:52:12Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![NightMachinary](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nightmachinary/32/14196_2.png) [@NightMachinary](https://discourse.julialang.org/u/NightMachinary)\
**Post date:** [March 10, 2021, 9:52am UTC](https://discourse.julialang.org/t/how-to-call-the-original-function-when-overriding-it/56865/1 "2021-03-10T09:52:12Z")

</div>

I want to override `Unitful.Base.show` to make it first simplify the quantity’s unit before showing it. I don’t know how to call the original overriden function though. What I have tried:

```julia
@defonce origShow = Unitful.Base.show
@eval Unitful function Base.show(io::IO, mime::MIME"text/plain", x::Quantity)
    Main.origShow(io, mime, upreferred(x))
end
@eval Unitful function Base.show(io::IO, mime::MIME"text/plain", x::Vector{<:Quantity})
    Main.origShow(io, mime, upreferred(x))
end

```

This code seems to cause an infinite loop and crashes Julia.

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [March 12, 2021, 4:48am UTC](https://discourse.julialang.org/t/how-to-call-the-original-function-when-overriding-it/56865/2 "2021-03-12T04:48:24Z")

</div>

You can use `invoke` to call a more generic method of the same function:

```julia
julia> function foo(x)
         println("generic foo")
       end
foo (generic function with 1 method)

julia> function foo(x::Int)
         invoke(foo, Tuple{Any}, x) # Call the x::Any version of `foo`
         println("specific foo")
       end
foo (generic function with 2 methods)

julia> foo(1)
generic foo
specific foo

```

---

<div class="post-metadata">

**Author:** ![NightMachinary](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nightmachinary/32/14196_2.png) [@NightMachinary](https://discourse.julialang.org/u/NightMachinary)\
**Post date:** [March 12, 2021, 8:10am UTC](https://discourse.julialang.org/t/how-to-call-the-original-function-when-overriding-it/56865/3 "2021-03-12T08:10:04Z")

</div>

I want to call that exact method, not a more generic version.

---

<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:** [March 12, 2021, 8:42am UTC](https://discourse.julialang.org/t/how-to-call-the-original-function-when-overriding-it/56865/4 "2021-03-12T08:42:59Z")

</div>

`invoke` should be able to do that if you replace the `Tuple{Any}` bit with the types you are specifying.  
I’m curious, though, why do you want to modify the function’s general behavior instead of using a new function like `newshow(io, mime, x) = Unitful.Base.show(io, mime, upreferred(x))` or just simplifying `x = upreferred(x)` wherever you can before calling `Unitful.Base.show`?

---

<div class="post-metadata">

**Author:** ![NightMachinary](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nightmachinary/32/14196_2.png) [@NightMachinary](https://discourse.julialang.org/u/NightMachinary)\
**Post date:** [March 12, 2021, 8:54am UTC](https://discourse.julialang.org/t/how-to-call-the-original-function-when-overriding-it/56865/5 "2021-03-12T08:54:20Z")

</div>

Then `invoke` would call my own newly defined function, no? The type signature of the new method is identical to its `super` method.

I don’t define a new function because I am trying to override how some values are displayed on the REPL. I don’t want to type `upreferred(...)` for everything.

---

<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:** [March 12, 2021, 9:39am UTC](https://discourse.julialang.org/t/how-to-call-the-original-function-when-overriding-it/56865/6 "2021-03-12T09:39:05Z")

</div>

Oh I see what you mean now. You wanted `origShow = Unitful.Base.show` to “copy” the function so you can use the function name for something else. But `origShow` actually sticks with the _name_ `Unitful.Base.show`. It works that way because when you dynamically change a function’s behavior i.e. by adding methods, you normally want methods that use that function to also change their behavior ([method invalidation](https://julialang.org/blog/2020/08/invalidations/#method_invalidation_what_is_it_and_when_does_it_happen)).

Your code just ended up being indirectly recursive, hence the suggestions to instead make another method with more specific types (to take precedence in dispatch) or a differently-named wrapper function. I think the only other straightforward way to make a different method is with a different number of arguments, like `show(blah, io, mime, x) = show(io, mime, upreferred(x))`. But I think you’re saying that the usual ways won’t work for this case because you’re not even calling this method directly, you’re trying to affect how the REPL decides to print something.

---

<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:** [March 12, 2021, 9:49am UTC](https://discourse.julialang.org/t/how-to-call-the-original-function-when-overriding-it/56865/7 "2021-03-12T09:49:28Z")

</div>

On 1.6 (or maybe only on master) you can use `invoke_in_world` for this:

```julia
julia> f(x) = print(x)
f (generic function with 1 method)

julia> w = Base.get_world_counter()
0x0000000000007418

julia> g(x) = Base.invoke_in_world(w, f, x)
g (generic function with 1 method)

julia> f(x) = begin; print("new: "); g(x); end
f (generic function with 1 method)

julia> f(2)
new: 2

```

---

<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:** [March 12, 2021, 10:03am UTC](https://discourse.julialang.org/t/how-to-call-the-original-function-when-overriding-it/56865/8 "2021-03-12T10:03:37Z")

</div>

World age always spooked me, shouldn’t be too surprised it powers such witchcraft!

---

<div class="post-metadata">

**Author:** ![NightMachinary](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nightmachinary/32/14196_2.png) [@NightMachinary](https://discourse.julialang.org/u/NightMachinary)\
**Post date:** [March 12, 2021, 11:22am UTC](https://discourse.julialang.org/t/how-to-call-the-original-function-when-overriding-it/56865/9 "2021-03-12T11:22:43Z")

</div>

Thaanks! It’s cool stuff like this that makes me love Julia.
