Invoke for functor methods

A minimal example demonstrating the use of invoke() might look something like the following.

julia> abstract type A end
       struct B <: A end
       f(a::A) = "hello world from $a"
       f(b::B) = invoke(f, Tuple{A}, b)
       f(B())
"hello world from B()"

Now imagine that rather than having a function f(), I want to use values of type A and B as functors instead. In this case, is there an analogous way to dispatch to the A functor?

(a::A)() = "hello world from $a"
(b::B)() = invoke( #= ??? =# , Tuple{A}, b)

This question is driven mainly by curiosity. In my actual code, I currently just dispatch to the above helper function f().

(a::A)() = f(a)

AFAICT this works fine, I’m just curious whether there would be a way to get rid of the additional helper function.

1 Like