# Why does JET say this leads to a dispatch error?

**URL:** <https://discourse.julialang.org/t/why-does-jet-say-this-leads-to-a-dispatch-error/109684>\
**Category:** New to Julia\
**Created:** [February 3, 2024, 8:29pm UTC](https://discourse.julialang.org/t/why-does-jet-say-this-leads-to-a-dispatch-error/109684 "2024-02-03T20:29:39Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [February 3, 2024, 8:29pm UTC](https://discourse.julialang.org/t/why-does-jet-say-this-leads-to-a-dispatch-error/109684/1 "2024-02-03T20:29:39Z")

</div>

Hello!

I am using `JET` to improve my package. I have a function looking like this:

```julia
function ResetArrays!(arrays...)
    @inbounds for array in arrays
        fill!(array,zero(eltype(array)))
    end
end

```

Which I feed in vectors of different types, for example `Vector{Float64}` and `Vector{SVector{3,Float64}}`. I then get the warning:

```julia
═════ 2 possible errors found ═════
┌ ResetArrays!(::Vector{Float64}, ::Vector{SVector{3, Float64}}, ::Vector{Float64}, ::Vector{Float64}, ::Vector{SVector{3, Float64}}, ::Vector{SVector{3, Float64}}) @ SPHExample.SimulationDataArrays c:\git\SPHExample\src\SimulationDataArrays.jl:20
│┌ fill!(dest::Vector{SVector{3, Float64}}, x::Float64) @ Base ./array.jl:393
││ runtime dispatch detected: convert(::SVector{3, Float64}, x::Float64)
│└────────────────────
│┌ fill!(dest::Vector{Float64}, x::SVector{3, Float64}) @ Base ./array.jl:393
││ runtime dispatch detected: convert(::Float64, x::SVector{3, Float64})
│└────────────────────

```

What is a runtime dispatch and why does this happen, and how would I fix it in Julia?

Kind regards

---

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [February 3, 2024, 8:56pm UTC](https://discourse.julialang.org/t/why-does-jet-say-this-leads-to-a-dispatch-error/109684/2 "2024-02-03T20:56:29Z")

</div>

Okay I realized that it is because that Julia is not able to at runtime determine the `eltype(array)`, when it gets mixed input. It works fine when all inputs are of the same type, then no error is detected.

Any smart way to fix it or I just have to split into two function calls?

EDIT: My current solution:

Enforce the type of variable arguments through `Vararg`.

```julia
function ResetArrays!(arrays::Vararg{Vector{T}}) where T
    @inbounds for array in arrays
        fill!(array,zero(eltype(array)))
    end
end

```

Split call into two function calls:

```julia
            # Clean up arrays, Vector{T} and Vector{SVector{3,T}} must be cleansed individually,
            # to avoid run time dispatch errors
            ResetArrays!(Kernel, dρdtI,dρdtIₙ⁺)
            ResetArrays!(KernelGradient, dvdtI, Acceleration)

```

Kind regards

---

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [February 3, 2024, 9:52pm UTC](https://discourse.julialang.org/t/why-does-jet-say-this-leads-to-a-dispatch-error/109684/3 "2024-02-03T21:52:50Z")

</div>

- Is that `@inbounds` really doing anything? It’s best to avoid those if you don’t really need them.

- I think you can just do `fill!.((k,d,p), 0)`

---

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [February 3, 2024, 9:57pm UTC](https://discourse.julialang.org/t/why-does-jet-say-this-leads-to-a-dispatch-error/109684/4 "2024-02-03T21:57:07Z")

</div>

> - Is that `@inbounds` really doing anything? It’s best to avoid those if you don’t really need them.

I think you are right, probably has no effect.

> I think you can just do `fill!.((k,d,p), 0)`

Yes, that is in a sense what I am doing, just having a function more? I think at least

Kind regards

---

<div class="post-metadata">

**Author:** ![matthias314](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@matthias314](https://discourse.julialang.org/u/matthias314)\
**Post date:** [February 4, 2024, 1:23am UTC](https://discourse.julialang.org/t/why-does-jet-say-this-leads-to-a-dispatch-error/109684/5 "2024-02-04T01:23:41Z")

</div>

You can also say

```julia
ResetArrays!(arrays...) = foreach(a -> fill!(a, zero(eltype(a))), arrays)

```

This seems to be faster than broadcasting.

---

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [February 4, 2024, 7:07am UTC](https://discourse.julialang.org/t/why-does-jet-say-this-leads-to-a-dispatch-error/109684/6 "2024-02-04T07:07:47Z")

</div>

Thank you. I am unsure why, but your solution reports no errors and seems to be able to dispatch on type correctly, so I marked it as solution.

Kind regards

---

<div class="post-metadata">

**Author:** ![ederag](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ederag/32/4106_2.png) [@ederag](https://discourse.julialang.org/u/ederag)\
**Post date:** [February 4, 2024, 9:43am UTC](https://discourse.julialang.org/t/why-does-jet-say-this-leads-to-a-dispatch-error/109684/7 "2024-02-04T09:43:45Z")

</div>

> I am unsure why, but your solution reports no errors and seems to be able to dispatch on type correctly

`a -> fill!(a, zero(eltype(a)))` is an anonymous function, that acts as a [function barrier](https://docs.julialang.org/en/v1/manual/performance-tips/#kernel-functions).

> EDIT: This was true, but definitely _not_ the explanation (see @matthias314 comment below). The following still triggers a “runtime dispatch detected” by JET.

In a less compact way, one could create a named function

```julia
function reset_array!(a)
    fill!(a, zero(eltype(a)))
end

```

and use it as

```julia
for a in arrays
    reset_array!(a)
end

```

---

<div class="post-metadata">

**Author:** ![matthias314](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@matthias314](https://discourse.julialang.org/u/matthias314)\
**Post date:** [February 4, 2024, 1:22pm UTC](https://discourse.julialang.org/t/why-does-jet-say-this-leads-to-a-dispatch-error/109684/8 "2024-02-04T13:22:40Z")

</div>

This is not the same. The point is that `foreach` (like `map`, `reduce` and friends) can unroll a loop over a tuple.

---

<div class="post-metadata">

**Author:** ![ederag](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ederag/32/4106_2.png) [@ederag](https://discourse.julialang.org/u/ederag)\
**Post date:** [February 4, 2024, 4:08pm UTC](https://discourse.julialang.org/t/why-does-jet-say-this-leads-to-a-dispatch-error/109684/9 "2024-02-04T16:08:28Z")

</div>

You are right, it looks like the function barrier is not enough;  
thanks a lot for the correction.

```julia-repl
julia> using JET

julia> function reset_array!(a)
           fill!(a, zero(eltype(a)))
       end

julia> function reset_all!(arrays)
           for a in arrays
               reset_array!(a)
           end
       end

julia> @report_opt reset_all!(Any[[1, 2], [1.0, 2.0]])
═════ 1 possible error found ═════
┌ reset_all!(arrays::Vector{Any}) @ Main ./REPL[14]:3
│ runtime dispatch detected: reset_array!(%19::Any)::Any
└────────────────────

```

But the `foreach` does not seem to work for JET either ?

```julia-repl

julia> function reset_all_2!(arrays)
           foreach(reset_array!, arrays)
       end

julia> @report_opt reset_all_2!(Any[[1, 2], [1.0, 2.0]])
┌ reset_all_2!(arrays::Vector{Any}) @ Main ./REPL[27]:2
│┌ foreach(f::typeof(reset_array!), itr::Vector{Any}) @ Base ./abstractarray.jl:3094
││ runtime dispatch detected: f::typeof(reset_array!)(%19::Any)::Any
│└────────────────────

```

---

<div class="post-metadata">

**Author:** ![matthias314](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@matthias314](https://discourse.julialang.org/u/matthias314)\
**Post date:** [February 4, 2024, 10:05pm UTC](https://discourse.julialang.org/t/why-does-jet-say-this-leads-to-a-dispatch-error/109684/10 "2024-02-04T22:05:16Z")

</div>

It’s crucial to use a tuple and not a vector. Try

```julia
reset_all_2!(([1, 2], ["a", "b"]))

```

(Remember that argument slurping in Julia gives a tuple.)

---

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [February 4, 2024, 11:00pm UTC](https://discourse.julialang.org/t/why-does-jet-say-this-leads-to-a-dispatch-error/109684/11 "2024-02-04T23:00:38Z")

</div>

It also works, if one uses `arrays...` i.e. making it varargs. I guess that is because the varargs is represented as a tuple internally?

---

<div class="post-metadata">

**Author:** ![matthias314](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@matthias314](https://discourse.julialang.org/u/matthias314)\
**Post date:** [February 4, 2024, 11:12pm UTC](https://discourse.julialang.org/t/why-does-jet-say-this-leads-to-a-dispatch-error/109684/12 "2024-02-04T23:12:48Z")

</div>

Right. This is exactly argument slurping. If you define a method of the form `function f(arrays...)`, then in the function body `arrays` is a tuple.
