# Priority in dispatch

I have a famility of functions

``````b(t, x::T) =
``````

called in a loop

``````for i in 1:n
t = t + dt
x = x + dt*f(t, x)
end
``````

It turns out that sometimes I’d need the index `i` as well and I’d like to define

``````f((i,s)::Tuple, x) = f(s, x)
for i in 1:n
t = t + dt
x = x + dt*f((i,s), x)
end
``````

Which creates ambiguity between all my `f(s, x::T)` and `f((i,s)::Tuple, x)`. Of course I could
play with the order of the arguments or use a different function name but just to be sure: is there a way do define

``````f((i,s)::Tuple, x) = f(s, x)
``````

such it has higher priority than (say)

``````f(s, x::Float64)
``````

but less than `f((i,s)::Tuple, x::Float64)`?

Instead of doing dispatch, can’t you just use a branch?

``````function f(s, x)
s isa Tuple && return _f(s, x)
return _f(s, x)
end

_f(s, x::Float64)  = ...
``````
1 Like

Or using dispatch:

``````f((i,s)::Tuple, x) = f(s,x)
f(s,x) = _f(s,x)
_f(s, x::Float64)  = ...
_f(s, ...) = ...
``````
1 Like