Hi, in my code, I have a structure similar to the below. The function that I call in running
should be determined by FooContainer.foo
, so it is clear to me that I have runtime dispatch here. What is the idiomatic way to work around this runtime dispatch here? I would guess that using one method for run_foo
in which I check the type of foo
, e.g. using isa(foo, Foo1)
resolves the problem, but is this the preferred solution? In reality, I’ll have a few (5-10) subtypes of FooType
and would like to dispatch on x
, which may have various types (say vector, matrix, float etc.), so I want to make sure that I get the right approach here…
using JET
abstract type FooType end
struct Foo1 <: FooType end
struct Foo2 <: FooType end
struct FooContainer
foo::FooType
end
function run_foo(foo::Foo1, x::Float64)
return x
end
function run_foo(foo::Foo2, x::Float64)
return x^2
end
function running(foo_container::FooContainer, x::Float64)
return run_foo(foo_container.foo, x)
end
rep = @report_opt running(FooContainer(Foo1()), 2.0)
show(rep)
which returns
═════ 1 possible error found ═════
┌ running(foo_container::FooContainer, x::Float64) @ (HIDDEN)
│ runtime dispatch detected: run_foo(%1::FooType, x::Float64)::Float64