# Method specificity/priority rules

**URL:** <https://discourse.julialang.org/t/method-specificity-priority-rules/132932>\
**Category:** New to Julia\
**Tags:** methods\
**Created:** [October 6, 2025, 10:54pm UTC](https://discourse.julialang.org/t/method-specificity-priority-rules/132932 "2025-10-06T22:54:33Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![raman\_kumar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raman_kumar/32/26782_2.png) [@raman\_kumar](https://discourse.julialang.org/u/raman_kumar)\
**Post date:** [October 6, 2025, 10:54pm UTC](https://discourse.julialang.org/t/method-specificity-priority-rules/132932/1 "2025-10-06T22:54:33Z")

</div>

Please explain methods priority or specificity in different cases. Where in Julia documentation is it explained? The following line and example in documentation are contradictory.

> [The order in which the methods are defined does not matter and neither is more specific than the other.](https://github.com/JuliaLang/julia/blob/fa66b63fc3421fa549f901afeb88d34dc88d06fd/doc/src/manual/methods.md?plain=1#L350C31-L351C41)

while here in [Methods · The Julia Language](https://docs.julialang.org/en/v1/manual/methods/#Parametric-Methods)

```julia-auto
julia> same_type(x::T, y::T) where {T} = true
same_type (generic function with 1 method)

julia> same_type(x,y) = false
same_type (generic function with 2 methods)

```

the first method is specific or preferred. Why not following code gives `false` as output if no method is specific or has priority?

```julia-auto
julia> same_type(1, 2)

```

* * *

I found this post but it explain different case. 👇

> [@Question about the selection mechanism of multiple dispatch](https://discourse.julialang.org/t/question-about-the-selection-mechanism-of-multiple-dispatch/11175/4):
>
> Yes. A type `T` is always more specific than `Union{T, X}` for some other type `X`, so a method defined with `::T` as its argument will be more specific than one with `::Union{T, X}` (assuming no change in the other arguments).
> 
> A few examples:
> 
> - `foo(::T)` is more specific than `foo(::Union{T, X})`
> - `foo(::T, ::X)` is more specific than `foo(::Union{T, X}, ::X)`
> - `foo(::T, ::X)` is more specific than `foo(::T, ::Union{T, X})`
> - `foo(::T, ::X)` is more specific than `foo(::Union{T, X}, ::Union{T, X})`
> 
> But there are ambiguous cases:
> 
> - `foo(::Union{T, X}, ::X)` is ambiguous with (neither more nor less specific than) `foo(::T, ::Union{T, X})`
> 
> If there is no unique most-specific method, then Julia will throw an ambiguity error, so there’s no need to worry about one method being arbitrarily preferred.

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [October 7, 2025, 12:38am UTC](https://discourse.julialang.org/t/method-specificity-priority-rules/132932/2 "2025-10-07T00:38:22Z")

</div>

A method _can_ be more specific than another, but when that’s not true, they risk a call throwing an ambiguity `MethodError`. You’re taking that quote out of a context where ambiguous methods were defined. I’ll save everyone else a click for an accurate quote:

> Here the call `g(2.0, 3.0)` could be handled by either the `g(::Float64, ::Any)` or the `g(::Any, ::Float64)` method. The order in which the methods are defined does not matter and neither is more specific than the other. In such cases, Julia raises a `MethodError` rather than arbitrarily picking a method.

---

<div class="post-metadata">

**Author:** ![raman\_kumar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raman_kumar/32/26782_2.png) [@raman\_kumar](https://discourse.julialang.org/u/raman_kumar)\
**Post date:** [October 7, 2025, 12:52pm UTC](https://discourse.julialang.org/t/method-specificity-priority-rules/132932/3 "2025-10-07T12:52:33Z")

</div>

> [@raman\_kumar](#):
>
> the first method is specific or preferred.

```julia-auto
julia> same_type(x::T, y::T) where {T} = true
same_type (generic function with 1 method)

julia> same_type(x,y) = false
same_type (generic function with 2 methods)

```

Why first method is preferred?

> [Such definitions correspond to methods whose type signatures are `UnionAll` types.](https://github.com/JuliaLang/julia/blob/31d34272bcd7b265a9bbd6ae0e3c00133f3c33eb/doc/src/manual/methods.md?plain=1#L412C1-L412C82)  
> is this line referring to the second method or both?

---

<div class="post-metadata">

**Author:** ![Liozou](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/liozou/32/35118_2.png) [@Liozou](https://discourse.julialang.org/u/Liozou)\
**Post date:** [October 7, 2025, 2:10pm UTC](https://discourse.julialang.org/t/method-specificity-priority-rules/132932/4 "2025-10-07T14:10:51Z")

</div>

Your two methods for function `same_type` have a different _signature_:

- The first one has signature `Tuple{typeof(same_type), T, T} where T`
- The second one has signature `Tuple{typeof(same_type), Any, Any}`

> **(here is a programmatic way of getting these method signatures)**
>
> The list of defined methods can be accessed with `methods`:
> 
> ```julia
> julia> methods(same_type)
> # 2 methods for generic function "same_type" from Main:
> [1] same_type(x::T, y::T) where T
> @ REPL[18]:1
> [2] same_type(x, y)
> @ REPL[23]:1
> 
> ```
> 
> To get the signature for each method, ask for the `sig` field:
> 
> ```julia
> julia> methods(same_type)[1].sig
> Tuple{typeof(same_type), T, T} where T
> 
> julia> methods(same_type)[2].sig
> Tuple{typeof(same_type), Any, Any}
> 
> ```

When a function is called, for example when you call `same_type(1, 2)`, the signature of your call will be compared to the signatures of all the methods of that function. The signature of your call is `Tuple{typeof(same_type), Int, Int}` in this particular case and it is a subtype of both method signatures:

```julia
julia> Tuple{typeof(same_type), Int, Int} <: Tuple{typeof(same_type), T, T} where T
true

julia> Tuple{typeof(same_type), Int, Int} <: Tuple{typeof(same_type), Any, Any}
true

```

so both methods are eligible. But the first one is more **specific** than the second one, because the signature of the first method is a subtype of the signature of the second method. Indeed:

```julia
julia> (Tuple{typeof(same_type), T, T} where T) <: Tuple{typeof(same_type), Any, Any}
true

```

while the converse is not true (otherwise the two signatures would be the same):

```julia
julia> Tuple{typeof(same_type), Any, Any} <: Tuple{typeof(same_type), T, T} where T
false

```

and thus, the first method is called and not the second.

The intuition is explained in [Methods · The Julia Language](https://docs.julialang.org/en/v1/manual/methods/#Defining-Methods) and the nitty-gritty details of subtyping can be found in [More about types · The Julia Language](https://docs.julialang.org/en/v1/devdocs/types/). In particular, the [last subsection](https://docs.julialang.org/en/v1/devdocs/types/) goes into more details about what is “method specificity”.  
Best is probably to watch Jeff Bezanson explaining the rules though. I highly recommend [https://www.youtube.com/live/TPuJsgyu87U?si=Nw\_1STjfpEPQS27w&t=985](https://www.youtube.com/live/TPuJsgyu87U?si=Nw_1STjfpEPQS27w&t=985) (the entire talk is great, the section starting at 16’25 is probably what you are most interested in for this particular question).

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [October 7, 2025, 10:19pm UTC](https://discourse.julialang.org/t/method-specificity-priority-rules/132932/5 "2025-10-07T22:19:16Z")

</div>

> [@Liozou](#):
>
> The signature of your call is `Tuple{typeof(same_type), Int, Int}` in this particular case and it is a subtype of both method signatures… But the first one is more **specific** than the second one, because the signature of the first method is a subtype of the signature of the second method.

Another call proving the subtyping is _strict_, so one method is more specific than instead of overwriting the other: `same_type(1, 2.0)`. `Tuple{typeof(same_type), Int, Float64}` does not subtype `Tuple{typeof(same_type), T, T} where T`, only `Tuple{typeof(same_type), Any, Any}`.

---

<div class="post-metadata">

**Author:** ![Liozou](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/liozou/32/35118_2.png) [@Liozou](https://discourse.julialang.org/u/Liozou)\
**Post date:** [October 7, 2025, 10:32pm UTC](https://discourse.julialang.org/t/method-specificity-priority-rules/132932/6 "2025-10-07T22:32:17Z")

</div>

EDIT: typo was fixed, this comment here is irrelevant.

> [@Benny](#):
>
> `Tuple{typeof(same_type), Int, Int}` does not subtype `Tuple{typeof(same_type), T, T} where T`

I’m not sure I understand: `Tuple{typeof(same_type), Int, Int}` does subtype `Tuple{typeof(same_type), T, T} where T`. That’s actually the first subtyping example I put in the previous answer.  
That comes from the fact that Tuple types are covariant in their parameters (it won’t work if you replace `Tuple` by another type). Moreover, `Int` is a concrete type so it’s valid for the diagonal rule (it won’t work if you replace `Int` by `Union{Int,Float64}` for example).~

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [October 7, 2025, 10:33pm UTC](https://discourse.julialang.org/t/method-specificity-priority-rules/132932/7 "2025-10-07T22:33:11Z")

</div>

Sorry, I made a typo, it didn’t fit the call. Thanks for catching it.

---

<div class="post-metadata">

**Author:** ![raman\_kumar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raman_kumar/32/26782_2.png) [@raman\_kumar](https://discourse.julialang.org/u/raman_kumar)\
**Post date:** [October 8, 2025, 10:26pm UTC](https://discourse.julialang.org/t/method-specificity-priority-rules/132932/8 "2025-10-08T22:26:15Z")

</div>

> Additionally, if 2 methods are defined with identical signatures, per type-equal, then they will instead \ ***by** compared by order of addition, such that the later method is more specific than the earlier

I think that this **by** after \* should be replaced by **be**.

> <https://github.com/JuliaLang/julia/blob/95c1718a799ed0bf3f13cb51603db6b48dc6b629/doc/src/devdocs/types.md?plain=1#L529C14-L530C22>

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [October 8, 2025, 11:08pm UTC](https://discourse.julialang.org/t/method-specificity-priority-rules/132932/9 "2025-10-08T23:08:11Z")

</div>

That’s indeed a likely typo, but you’re taking things out of context again. That’s talking about a different specificity concept for internal method tables, not method dispatch. If you evaluate 2 methods with identical signatures, the recent one overwrites the previous one on the language level; how that happens internally are irrelevant to you. Generally speaking, the devdocs don’t specify the language, it describes parts of the implementation. I’ll hazard a guess that you’re searching by keywords like “specificity”, and you need to read and understand the surrounding context to avoid mixing up different topics.

---

<div class="post-metadata">

**Author:** ![raman\_kumar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raman_kumar/32/26782_2.png) [@raman\_kumar](https://discourse.julialang.org/u/raman_kumar)\
**Post date:** [October 13, 2025, 1:49pm UTC](https://discourse.julialang.org/t/method-specificity-priority-rules/132932/10 "2025-10-13T13:49:02Z")

</div>

> [In this case the result is `5`, because `f(1,2)` invokes the first method of `f` above.](https://docs.julialang.org/en/v1/manual/methods/#Note-on-Optional-and-keyword-Arguments)

This line is a bit unnecessary or confusing. Which first method above?

**f(a,b) = a+2b**

```julia-auto
f(a,b) = a+2b
f(a) = f(a,2)
f() = f(1,2)

```

or **f( )**

```julia-auto
julia> f(a=1,b=2) = a+2b
f (generic function with 3 methods)

julia> methods(f)
# 3 methods for generic function "f" from Main:
 [1] f()
     @ REPL[1]:1
 [2] f(a, b)
     @ REPL[1]:1
 [3] f(a)
     @ REPL[1]:1

```

> In other words, optional arguments are tied to a function, not to any specific method of that function. It depends on the types of the optional arguments which method is invoked.

You mean optional arguments values will be available for all methods?

---

<div class="post-metadata">

**Author:** ![Vasily\_Pisarev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vasily_pisarev/32/7929_2.png) [@Vasily\_Pisarev](https://discourse.julialang.org/u/Vasily_Pisarev)\
**Post date:** [October 13, 2025, 2:54pm UTC](https://discourse.julialang.org/t/method-specificity-priority-rules/132932/11 "2025-10-13T14:54:21Z")

</div>

> [@raman\_kumar](#):
>
> This line is a bit unnecessary or confusing. Which first method above?

That refers to the manual text, so the first one of

```julia-auto
f(a,b) = a+2b
f(a) = f(a,2)
f() = f(1,2)

```

The ordering returned by `methods` is arbitrary.

---

<div class="post-metadata">

**Author:** ![adienes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adienes/32/37459_2.png) [@adienes](https://discourse.julialang.org/u/adienes)\
**Post date:** [January 6, 2026, 3:52pm UTC](https://discourse.julialang.org/t/method-specificity-priority-rules/132932/12 "2026-01-06T15:52:50Z")

</div>

> [@Vasily\_Pisarev](#):
>
> The ordering returned by `methods` is arbitrary.

Is that true? I thought it is ordered by weakly decreasing specificity (among each arity)

there is an [old comment](https://github.com/JuliaLang/julia/issues/3025#issuecomment-314470929) that suggests this to be the case at least, and some eyeball tests seem to confirm

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [January 6, 2026, 6:34pm UTC](https://discourse.julialang.org/t/method-specificity-priority-rules/132932/13 "2026-01-06T18:34:20Z")

</div>

I think he means `methods` doesn’t specify an order. If it did, then it’d have to keep doing that even if method tables somehow change so that the order is less performant or convenient to compute.

---

<div class="post-metadata">

**Author:** ![Vasily\_Pisarev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vasily_pisarev/32/7929_2.png) [@Vasily\_Pisarev](https://discourse.julialang.org/u/Vasily_Pisarev)\
**Post date:** [January 6, 2026, 6:38pm UTC](https://discourse.julialang.org/t/method-specificity-priority-rules/132932/14 "2026-01-06T18:38:53Z")

</div>

@Benny is right, I meant the order is not fully defined.

For example, in my installation order of method definitions affects the `methods` output:

```julia-auto
julia> f(a, b) = a + 2b;

julia> f(a) = a + 4;

julia> f() = 5;

julia> methods(f)
# 3 methods for generic function "f" from Main:
 [1] f()
     @ REPL[3]:1
 [2] f(a)
     @ REPL[2]:1
 [3] f(a, b)
     @ REPL[1]:1

```

```julia-auto
julia> f() = 5;

julia> f(a) = a + 4;

julia> f(a, b) = a + 2b;

julia> methods(f)
# 3 methods for generic function "f" from Main:
 [1] f()
     @ REPL[1]:1
 [2] f(a, b)
     @ REPL[3]:1
 [3] f(a)
     @ REPL[2]:1

```
