# Getting the closest method or dispatching method with parameter types without calling

**URL:** <https://discourse.julialang.org/t/getting-the-closest-method-or-dispatching-method-with-parameter-types-without-calling/27057>\
**Category:** General Usage\
**Created:** [August 1, 2019, 12:48am UTC](https://discourse.julialang.org/t/getting-the-closest-method-or-dispatching-method-with-parameter-types-without-calling/27057 "2019-08-01T00:48:06Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Qiyamah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/qiyamah/32/2660_2.png) [@Qiyamah](https://discourse.julialang.org/u/Qiyamah)\
**Post date:** [August 1, 2019, 12:48am UTC](https://discourse.julialang.org/t/getting-the-closest-method-or-dispatching-method-with-parameter-types-without-calling/27057/1 "2019-08-01T00:48:06Z")

</div>

Hi,

```julia
julia> f(x::Int,y::Float64) = x*y
f (generic function with 1 method)

julia> f(x::Float64,y::Int) = x/y
f (generic function with 2 methods)

julia> f(x::Float64,y::Float64) = x+y
f (generic function with 3 methods)

julia> f(1,1)
ERROR: MethodError: no method matching f(::Int64, ::Int64)
Closest candidates are:
  f(::Float64, ::Int64) at none:1
  f(::Int64, ::Float64) at none:1
Stacktrace:
 [1] top-level scope at none:0

```

I want to do something like this  
methods = get\_closest\_method(f,typeof(1),typeof(1))

methods will have the correct dispatch or the closest methods for dispatch. Or an object reporting disagreeing parameter types.

Thanks

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [August 1, 2019, 12:52am UTC](https://discourse.julialang.org/t/getting-the-closest-method-or-dispatching-method-with-parameter-types-without-calling/27057/2 "2019-08-01T00:52:04Z")

</div>

If you’re getting a method error, that means there is no unique closest method. Are you asking about how to use reflection or just trying to make the method error go away?

---

<div class="post-metadata">

**Author:** ![Qiyamah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/qiyamah/32/2660_2.png) [@Qiyamah](https://discourse.julialang.org/u/Qiyamah)\
**Post date:** [August 1, 2019, 12:59am UTC](https://discourse.julialang.org/t/getting-the-closest-method-or-dispatching-method-with-parameter-types-without-calling/27057/3 "2019-08-01T00:59:51Z")

</div>

I am able to dispatch to a fallback function f(args…;kwargs…)  
However I would like to get my closest candidate(s) among the methods in the fallback method using reflection or any programmatic way. The ones shown in the method error are the closest I assume.  
What I would like is also which parameters didn’t match as well in type.

Thanks

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [August 1, 2019, 1:10am UTC](https://discourse.julialang.org/t/getting-the-closest-method-or-dispatching-method-with-parameter-types-without-calling/27057/4 "2019-08-01T01:10:13Z")

</div>

The closest method list is very much a heuristic and shouldn’t be relied on for anything in your code.

---

<div class="post-metadata">

**Author:** ![Qiyamah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/qiyamah/32/2660_2.png) [@Qiyamah](https://discourse.julialang.org/u/Qiyamah)\
**Post date:** [August 1, 2019, 1:11am UTC](https://discourse.julialang.org/t/getting-the-closest-method-or-dispatching-method-with-parameter-types-without-calling/27057/5 "2019-08-01T01:11:29Z")

</div>

I assume there is a way to sort the methods according to number of parameters didn’t type match  
and report those non-matching types in each candidate.

right?

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [August 1, 2019, 1:16am UTC](https://discourse.julialang.org/t/getting-the-closest-method-or-dispatching-method-with-parameter-types-without-calling/27057/6 "2019-08-01T01:16:47Z")

</div>

Not easily. You would have to look at the existing code for the closest method list and modify it.

---

<div class="post-metadata">

**Author:** ![Qiyamah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/qiyamah/32/2660_2.png) [@Qiyamah](https://discourse.julialang.org/u/Qiyamah)\
**Post date:** [August 1, 2019, 1:19am UTC](https://discourse.julialang.org/t/getting-the-closest-method-or-dispatching-method-with-parameter-types-without-calling/27057/7 "2019-08-01T01:19:55Z")

</div>

Can you point me to that code please?  
Thanks

---

<div class="post-metadata">

**Author:** ![Qiyamah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/qiyamah/32/2660_2.png) [@Qiyamah](https://discourse.julialang.org/u/Qiyamah)\
**Post date:** [August 1, 2019, 2:02am UTC](https://discourse.julialang.org/t/getting-the-closest-method-or-dispatching-method-with-parameter-types-without-calling/27057/8 "2019-08-01T02:02:03Z")

</div>

Is it possible to take action regarding dispatch taking that method error in account using reflection?  
What were you going to suggest?

Thanks

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [August 1, 2019, 2:03am UTC](https://discourse.julialang.org/t/getting-the-closest-method-or-dispatching-method-with-parameter-types-without-calling/27057/9 "2019-08-01T02:03:05Z")

</div>

I would suggest implementing the missing method rather than trying to guess.

---

<div class="post-metadata">

**Author:** ![Qiyamah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/qiyamah/32/2660_2.png) [@Qiyamah](https://discourse.julialang.org/u/Qiyamah)\
**Post date:** [August 1, 2019, 2:05am UTC](https://discourse.julialang.org/t/getting-the-closest-method-or-dispatching-method-with-parameter-types-without-calling/27057/10 "2019-08-01T02:05:38Z")

</div>

Is there an example where someone uses method error and makes a dispatch with that info?

Thanks

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [August 1, 2019, 2:35am UTC](https://discourse.julialang.org/t/getting-the-closest-method-or-dispatching-method-with-parameter-types-without-calling/27057/11 "2019-08-01T02:35:28Z")

</div>

Not that I can think of. What’s the issue with just defining the method?

---

<div class="post-metadata">

**Author:** ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)\
**Post date:** [August 2, 2019, 6:51pm UTC](https://discourse.julialang.org/t/getting-the-closest-method-or-dispatching-method-with-parameter-types-without-calling/27057/12 "2019-08-02T18:51:33Z")

</div>

Don’t try to fight the system, join it 😄. Those ambiguity errors are there for good reason, and the way to stop them is to write code that doesn’t have them. If you’re getting them all the time, you may be doing something else wrong, like declaring types in places you shouldn’t, etc. There’s a whole section in the manual that may help, see [https://docs.julialang.org/en/latest/manual/methods/#man-method-design-ambiguities-1](https://docs.julialang.org/en/latest/manual/methods/#man-method-design-ambiguities-1).
