# Problems with dispatching of Base.iterate

**URL:** <https://discourse.julialang.org/t/problems-with-dispatching-of-base-iterate/42367>\
**Category:** New to Julia\
**Created:** [July 1, 2020, 1:44pm UTC](https://discourse.julialang.org/t/problems-with-dispatching-of-base-iterate/42367 "2020-07-01T13:44:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Isaia\_Nisoli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/isaia_nisoli/32/12594_2.png) [@Isaia\_Nisoli](https://discourse.julialang.org/u/Isaia_Nisoli)\
**Post date:** [July 1, 2020, 1:44pm UTC](https://discourse.julialang.org/t/problems-with-dispatching-of-base-iterate/42367/1 "2020-07-01T13:44:49Z")

</div>

The situation is the following: I’m defining some iterators with the following structure

```julia
module foo

module Generic
import Base: iterate

abstract type Gen

struct iterator{G} where {G<:Gen}
end

Base.iterate(S::iterator{Gen}, state) = ...

end

module Specific
using ..Generic
import Base:iterate

struct Spec<:Gen
end

Base.iterate(S::iterator{Spec}, state)=...
end

```

But the compiler seems like it cannot find Base.iterate(S::iterator{Spec}, state), i.e., it tries to dispatch on the Generic method and not on the Specific.

Do you have any clues?

Best wishes  
Isaia

---

<div class="post-metadata">

**Author:** ![Isaia\_Nisoli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/isaia_nisoli/32/12594_2.png) [@Isaia\_Nisoli](https://discourse.julialang.org/u/Isaia_Nisoli)\
**Post date:** [July 1, 2020, 3:38pm UTC](https://discourse.julialang.org/t/problems-with-dispatching-of-base-iterate/42367/2 "2020-07-01T15:38:04Z")

</div>

I solved it, I will leave it here so if anyone has the same problem, there is an answer.  
The main clue is that, if we have an

```julia
abstract type A 

```

I was dispatching as

```julia
f(x::A)

```

which is different than

```julia
f(x::T) where {T<:A}

```

so the dispatcher could not find the right method.

Best wishes

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [July 1, 2020, 5:20pm UTC](https://discourse.julialang.org/t/problems-with-dispatching-of-base-iterate/42367/3 "2020-07-01T17:20:06Z")

</div>

> [@Isaia\_Nisoli](#):
>
> ```julia
> f(x::A)
> 
> ```
> 
> which is different than
> 
> ```julia
> f(x::T) where {T<:A}
> 
> ```

It’s not different–those method definitions are exactly the same:

```julia
julia> abstract type A end

julia> f(x::A) = 1
f (generic function with 1 method)

julia> f(x::T) where {T <: A} = 2
f (generic function with 1 method)

```

Note how `f` still has exactly one method, because `A` and `T where T <: A` are exactly identical:

```julia
julia> (T where T <: A) === A
true

```

Reading between the lines in your example, I wonder if you were actually doing something like:

```julia
julia> struct Container{T}; end

julia> g(x::Container{A}) = 1
g (generic function with 1 method)

```

vs.

```julia
julia> g(x::Container{T}) where {T <: A} = 2
g (generic function with 2 methods)

```

Because parametric types in Julia are not [covariant](https://docs.julialang.org/en/v1/manual/types/#Parametric-Composite-Types-1), `Container{A}` and `Container{T} where {T <: A}` are indeed different.

This comes up often in arrays, since an `Array{Real}` is an array in which each element can be any (different) kind of `Real`, while an `Array{T} where {T <: Real}` is an array with an element type which is itself some subtype of `Real`. Thus, `Array{Int}` is a subtype of `Array{T} where {T <: Real}`, but `Array{Int}` is not a subtype of `Array{Real}`. Invariance can be hard to wrap your head around the first time–I had to read this section of the manual a few times before I got it: [Types · The Julia Language](https://docs.julialang.org/en/v1/manual/types/#Parametric-Composite-Types-1)

---

<div class="post-metadata">

**Author:** ![Isaia\_Nisoli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/isaia_nisoli/32/12594_2.png) [@Isaia\_Nisoli](https://discourse.julialang.org/u/Isaia_Nisoli)\
**Post date:** [July 1, 2020, 9:05pm UTC](https://discourse.julialang.org/t/problems-with-dispatching-of-base-iterate/42367/4 "2020-07-01T21:05:42Z")

</div>

Yes, you’re right, it was a parametric type! I missed the huge yellow warning in the page you linked… Thank you for the nice answer, it cleared up things.
