# How to check what method is used by dispatch?

**URL:** <https://discourse.julialang.org/t/how-to-check-what-method-is-used-by-dispatch/131178>\
**Category:** General Usage\
**Created:** [July 29, 2025, 5:11pm UTC](https://discourse.julialang.org/t/how-to-check-what-method-is-used-by-dispatch/131178 "2025-07-29T17:11:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![lrnv](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lrnv/32/19373_2.png) [@lrnv](https://discourse.julialang.org/u/lrnv)\
**Post date:** [July 29, 2025, 5:11pm UTC](https://discourse.julialang.org/t/how-to-check-what-method-is-used-by-dispatch/131178/1 "2025-07-29T17:11:36Z")

</div>

Hey,

Say I have an abstract type `A` and many subtypes `B, C, D`:

```julia
abstract type A end
struct B<:A end
struct C<:A end

should_return_one(::A) = 1
should_return_one(x::B) = # something else. 
# remark that, on purpose, I did not overwrote the method for x::C

```

Suppose now that for testing reasons, I want to ensure that the subtypes are implementing the method correctly. I am testing it like that :

```julia-auto
objects = (B(), C()) # there are a lot more objects here. 
for o in objets 
    @test should_return_one(o) == 1
end

```

Now, if the method is very costly, testing the method for `C` is not worth it because it will dispatch to the generic version. Thus, I’d like to only test when object do indeed overwrite the generic method:

```julia-auto
objects = (B(), C()) # there are a lot more objects here. 
for o in objets
    condition = # the call will not dispatch to should_return_one(::A)
    if condition
        @test should_return_one(o) == 1
    end
end

```

How can i write this condition ?

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [July 29, 2025, 5:23pm UTC](https://discourse.julialang.org/t/how-to-check-what-method-is-used-by-dispatch/131178/2 "2025-07-29T17:23:39Z")

</div>

Hopefully there are better ways to do this, but one option is to use `which` and check the (internal, private, unreliable, use at your own risk) field `sig`:

```julia-auto
julia> using Test, InteractiveUtils

julia> abstract type A end

julia> struct B<:A end

julia> struct C<:A end

julia> should_return_one(::A) = 1
should_return_one (generic function with 2 methods)

julia> should_return_one(x::B) = 2
should_return_one (generic function with 2 methods)

julia> which(should_return_one, (B,)).sig
Tuple{typeof(should_return_one), B}

julia> which(should_return_one, (C,)).sig
Tuple{typeof(should_return_one), A}

```

---

<div class="post-metadata">

**Author:** ![lrnv](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lrnv/32/19373_2.png) [@lrnv](https://discourse.julialang.org/u/lrnv)\
**Post date:** [July 29, 2025, 5:26pm UTC](https://discourse.julialang.org/t/how-to-check-what-method-is-used-by-dispatch/131178/3 "2025-07-29T17:26:02Z")

</div>

How, `which` sound nice indeed… What about simply `which(should_return_one, (A,)) == which(should_return_one, (B,))` ? To avoid internals.

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [July 29, 2025, 5:27pm UTC](https://discourse.julialang.org/t/how-to-check-what-method-is-used-by-dispatch/131178/4 "2025-07-29T17:27:44Z")

</div>

I guess that’d work (I didn’t realise `which` accepts abstract types, but it’s documented to do so, that’s good)
