# Overloading parametric methods - avoiding ambiguity

**URL:** <https://discourse.julialang.org/t/overloading-parametric-methods-avoiding-ambiguity/3181>\
**Category:** General Usage\
**Tags:** question\
**Created:** [April 12, 2017, 2:05pm UTC](https://discourse.julialang.org/t/overloading-parametric-methods-avoiding-ambiguity/3181 "2017-04-12T14:05:51Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ward9250](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ward9250/32/42768_2.png) [@Ward9250](https://discourse.julialang.org/u/Ward9250)\
**Post date:** [April 12, 2017, 2:05pm UTC](https://discourse.julialang.org/t/overloading-parametric-methods-avoiding-ambiguity/3181/1 "2017-04-12T14:05:51Z")

</div>

Hi.

I have an example over overloading a parametric method that results in an ambiguity error I’d like to avoid.

Say I have the following default method:

```julia

abstract Site
immutable NaiveCount

@inline function count_algorithm{T<:Site,A,B}(::Type{T}, ::Type{BioSequence{A}}, ::Type{BioSequence{B}})
    return NaiveCount
end

```

So for any two BioSequences the function always returns NaieveCount.

Now say I want to add more specific behaviour for concrete types that \<: Site, where if A and B are the same type, then `count_algorithm` returns something else. I thought this could be achieved by the following:

```julia
immutable BitparCount end

immutable Mismatch <: Site end
@inline function count_algorithm{A}(::Type{Mismatch}, ::Type{A}, ::Type{A})
    return BitparCount
end

```

However this results in an ambiguity:

```julia
julia> # I expect the second method to be the one that applies to this combination of arguments:
julia> count_algorithm(Mismatch, BioSequence{DNAAlphabet{4}}, BioSequence{DNAAlphabet{4}})
ERROR: MethodError: count_algorithm(::Type{Bio.Seq.Mismatch}, ::Type{Bio.Seq.BioSequence{Bio.Seq.DNAAlphabet{4}}}, ::Type{Bio.Seq.BioSequence{Bio.Seq.DNAAlphabet{4}}}) is ambiguous. Candidates:
  count_algorithm{A}(::Type{Bio.Seq.Mismatch}, ::Type{A}, ::Type{A}) at REPL[13]:2
  count_algorithm{T<:Bio.Seq.Site,A,B}(::Type{T}, ::Type{Bio.Seq.BioSequence{A}}, ::Type{Bio.Seq.BioSequence{B}}) at REPL[9]:2

```

How do I achieve this behaviour whilst avoiding the ambiguity?

---

<div class="post-metadata">

**Author:** ![jameson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jameson/32/23_2.png) [@jameson](https://discourse.julialang.org/u/jameson)\
**Post date:** [April 12, 2017, 3:50pm UTC](https://discourse.julialang.org/t/overloading-parametric-methods-avoiding-ambiguity/3181/2 "2017-04-12T15:50:25Z")

</div>

Including a constraint for `A` in the added definition should make this work:

```julia
count_algorithm{A<:BioSequence}(::Type{Mismatch}, ::Type{A}, ::Type{A})

```
