Storing a dispatched method for later use


#1

Suppose I have several method definitions with the same function name. Is it possible to dispatch a method for specific arguments, store the dispatched function, and use it later?

For example, suppose I have types and methods for the types as follows:

abstract type MyAbs end

struct MyType1 <: MyAbs end
struct MyType2 <: MyAbs end
struct MyType3 <: MyAbs end
struct MyType4 <: MyAbs end

myfun(i::Int, m::MyType1) = ...
myfun(i::Int, m::MyType2) = ...
myfun(i::Int, m::MyType3) = ...
myfun(i::Int, m::MyType4) = ...

Now, I want to do something like this:

mvec = [MyType1(), MyType2(), MyType3(), MyType4()]  # Vector{MyAbs}

for m = mvec
    f = (dispatch myfun for typeof(m))  # store dispatched myfun in f
    for i = 1:100
        f(i, m)  # execute dispatched method without dispatching it every iteration
    end   
end

#2

Use a function barrier?