Search code examples
juliaabstractoverloading

Is it possible to call an overloaded function from overwriting function in Julia?


The problem is the following:

I have an abstract type MyAbstract and derived composite types MyType1 and MyType2:

abstract type MyAbstract end

struct MyType1 <: MyAbstract
    somestuff
end

struct MyType2 <: MyAbstract
    someotherstuff
end

I want to specify some general behaviour for objects of type MyAbstract, so I have a function

function dosth(x::MyAbstract)
    println(1) # instead of something useful
end

This general behaviour suffices for MyType1 but when dosth is called with an argument of type MyType2, I want some additional things to happen that are specific for MyType2 and, of course, I want to reuse the existing code, so I tried the following, but it did not work:

function dosth(x::MyType2)
    dosth(x::MyAbstract)
    println(2)
end

x = MyType2("")
dosth(x) # StackOverflowError

This means Julia did not recognize my attempt to treat x like its "supertype" for some time.

Is it possible to call an overloaded function from the overwriting function in Julia? How can I elegantly solve this problem?


Solution

  • You can use the invoke function

    function dosth(x::MyType2)
        invoke(dosth, Tuple{MyAbstract}, x)
        println(2)
    end
    

    With the same setup, this gives the follow output instead of a stack overflow:

    julia> dosth(x)
    1
    2
    

    There's a currently internal and experimental macro version of invoke which can be called like this:

    function dosth(x::MyType2)
        Base.@invoke dosth(x::MyAbstract)
        println(2)
    end
    

    This makes the calling syntax quite close to what you wrote.