# Runtime dispatch in struct that contains struct

**URL:** https://discourse.julialang.org/t/runtime-dispatch-in-struct-that-contains-struct/118840
**Category:** New to Julia
**Tags:** runtime-dispatch
**Created:** [August 31, 2024, 12:02pm UTC](https://discourse.julialang.org/t/runtime-dispatch-in-struct-that-contains-struct/118840 "2024-08-31T12:02:43Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![JADekker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jadekker/32/210281_2.png) [@JADekker](https://discourse.julialang.org/u/JADekker)
#### Post date: [August 31, 2024, 12:02pm UTC](https://discourse.julialang.org/t/runtime-dispatch-in-struct-that-contains-struct/118840/1 "2024-08-31T12:02:43Z")

</div>

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…

```julia
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

```julia
═════ 1 possible error found ═════
┌ running(foo_container::FooContainer, x::Float64) @ (HIDDEN)
│ runtime dispatch detected: run_foo(%1::FooType, x::Float64)::Float64

```

---

<div class="post-metadata">

### Author: ![Tarny\_GG\_Channie](https://avatars.discourse-cdn.com/v4/letter/t/3bc359/32.png) [@Tarny\_GG\_Channie](https://discourse.julialang.org/u/Tarny_GG_Channie)
#### Post date: [August 31, 2024, 1:50pm UTC](https://discourse.julialang.org/t/runtime-dispatch-in-struct-that-contains-struct/118840/2 "2024-08-31T13:50:36Z")

</div>

> [@JADekker](#):
>
> ```julia
> struct FooContainer 
> foo::FooType
> end
> 
> ```

Here.  
You need to fully parameterize the FooContainer type. As of now, the FooContainer type can hold any type that is of FooType as foo.

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [August 31, 2024, 1:56pm UTC](https://discourse.julialang.org/t/runtime-dispatch-in-struct-that-contains-struct/118840/3 "2024-08-31T13:56:52Z")

</div>

See the [performance tips: avoid fields with abstract type](https://docs.julialang.org/en/v1/manual/performance-tips/#Avoid-fields-with-abstract-type).

---

<div class="post-metadata">

### Author: ![JADekker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jadekker/32/210281_2.png) [@JADekker](https://discourse.julialang.org/u/JADekker)
#### Post date: [August 31, 2024, 2:05pm UTC](https://discourse.julialang.org/t/runtime-dispatch-in-struct-that-contains-struct/118840/4 "2024-08-31T14:05:48Z")

</div>

I see, thank you! So if I simply parametrise the type here, I should be fine? That would be very nice! I’ll check once I have time and mark this as the solution afterwards!

Is there some tooling that automatically identifies such issues? (under-parametrised types?)

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [August 31, 2024, 2:18pm UTC](https://discourse.julialang.org/t/runtime-dispatch-in-struct-that-contains-struct/118840/5 "2024-08-31T14:18:38Z")

</div>

> [@JADekker](#):
>
> So if I simply parametrise the type here, I should be fine?

Yes.

---

<div class="post-metadata">

### Author: ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)
#### Post date: [August 31, 2024, 2:33pm UTC](https://discourse.julialang.org/t/runtime-dispatch-in-struct-that-contains-struct/118840/6 "2024-08-31T14:33:34Z")

</div>

It really depends. Generally speaking, you’ll probably want

> [@stevengj](#):
>
> > [@JADekker](#):
> >
> > So if I simply parametrise the type here, I should be fine?
> 
> Yes.

Just to expand on this, you’ll only be okay if the compiler can know ahead of time what the parameter is.

For example, if you have a vector with `FooContainer`s that have many different `FooType`s inside of them, then paramerizing wont help at all.

In cases like that, I would instead recommend an approach like [SumTypes.jl](https://github.com/MasonProtter/SumTypes.jl) or Moshi.jl.

These packages specialize in taking situations where you have a finite number of options and writing code that handles each of those options in a more efficient way than dispatch can. There’s tradeoffs like a lack of extensibility, but the advantages can be quite nice too.
