# Why the following two methods are ambiguous?

**URL:** <https://discourse.julialang.org/t/why-the-following-two-methods-are-ambiguous/28997>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [September 20, 2019, 11:45am UTC](https://discourse.julialang.org/t/why-the-following-two-methods-are-ambiguous/28997 "2019-09-20T11:45:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![amartin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amartin/32/10388_2.png) [@amartin](https://discourse.julialang.org/u/amartin)\
**Post date:** [September 20, 2019, 11:45am UTC](https://discourse.julialang.org/t/why-the-following-two-methods-are-ambiguous/28997/1 "2019-09-20T11:45:33Z")

</div>

Dear All,

I am pretty new to Julia. Going over the documentation (v1.1.1), I am struggling at Section 19.10, trying to understand why the following two method definitions for the generic function f are ambiguous:

```julia
f(::AbstractArray{T},::Number) where {T} = ...
f(::Array{T}, ::T) where {T} = ...

```

e.g., for f([1,2,3],1). As far as I understand, this function call matches the second method with method type parameter value T=Int64. While it also matches the first method with T=Int64, isn’t the second method with T=Int64 a more specific variant of the first method with T=Int64? (as Array{Int64} \<: AbstractArray{Int64} and Int64 \<: Number).

I am almost sure that I am missunderstanding sthg related to parametric methods/composite types, but I am not able to actually figure out what exactly.

Thanks a lot for your help!  
Best regards,  
Alberto F. Martín.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [September 20, 2019, 12:07pm UTC](https://discourse.julialang.org/t/why-the-following-two-methods-are-ambiguous/28997/2 "2019-09-20T12:07:31Z")

</div>

Think of the two method signatures each defining a set of applicable argument types.

Here, neither set is a subset of the other, because the `T` in the second one does not have to be `<: Number`. The fix is to define the second method as

```julia
f(::Array{T}, ::T) where {T <: Number} = 2

```

---

<div class="post-metadata">

**Author:** ![amartin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amartin/32/10388_2.png) [@amartin](https://discourse.julialang.org/u/amartin)\
**Post date:** [September 20, 2019, 12:38pm UTC](https://discourse.julialang.org/t/why-the-following-two-methods-are-ambiguous/28997/3 "2019-09-20T12:38:39Z")

</div>

> [@Tamas\_Papp](#):
>
> Think of the two method signatures each defining a set of applicable argument types.
> 
> Here, neither set is a subset of the other, because the `T` in the second one does not have to be `<: Number` . The fix is to define the second method as
> 
> ```julia
> 
> ```

Dear @Tamas_Papp,

Thanks for your insightful response! It was very helpful.

I think I got it … just to double-check, you resolve the amibiguity by constraining the set of applicable argument types to the second parameter of the second method to be a subset of those of the second parameter of the first method … Right?

Best regards,  
Alberto.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [September 20, 2019, 1:23pm UTC](https://discourse.julialang.org/t/why-the-following-two-methods-are-ambiguous/28997/4 "2019-09-20T13:23:45Z")

</div>

I prefer to think of the set of all argument types (ie a set of `Tuple`s), combined for all positional arguments. Here I need to narrow the second method so that

```julia
T1 = Tuple{AbstractArray{T,N} where N, S} where {T, S <: Number}
T2 = Tuple{Array{T,N} where N, T} where {T <: Number}
T2 <: T1 # true

```

Those `where`s inside are [explained here](https://docs.julialang.org/en/v1.4-dev/manual/types/#UnionAll-Types-1), `Tuple`s [here](https://docs.julialang.org/en/v1.4-dev/manual/types/#Tuple-Types-1).
