# Obtaining the type of the state object returned by Iterate

**URL:** <https://discourse.julialang.org/t/obtaining-the-type-of-the-state-object-returned-by-iterate/59190>\
**Category:** Performance\
**Tags:** performance, type, parametric-types, type-stability, iterators\
**Created:** [April 13, 2021, 1:26pm UTC](https://discourse.julialang.org/t/obtaining-the-type-of-the-state-object-returned-by-iterate/59190 "2021-04-13T13:26:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Olof\_Salberger](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/olof_salberger/32/4850_2.png) [@Olof\_Salberger](https://discourse.julialang.org/u/Olof_Salberger)\
**Post date:** [April 13, 2021, 1:26pm UTC](https://discourse.julialang.org/t/obtaining-the-type-of-the-state-object-returned-by-iterate/59190/1 "2021-04-13T13:26:53Z")

</div>

An issue I’ve run into is that if you have a container type T, in general it isn’t possible to obtain the type of state returned by iterate in the same way as you can get eltype(T) since there is no (let’s call it iterate\_statetype(T) ) function in the iterator protocol.

This makes it difficult to efficiently save the state object returned by iterate in a container, for example if you are rewriting a recursive algorithm as an iterative one and are pushing the “stack frames” into a vector.

---

<div class="post-metadata">

**Author:** ![contradict](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@contradict](https://discourse.julialang.org/u/contradict)\
**Post date:** [April 13, 2021, 3:53pm UTC](https://discourse.julialang.org/t/obtaining-the-type-of-the-state-object-returned-by-iterate/59190/2 "2021-04-13T15:53:12Z")

</div>

[`Base.Iterators.Stateful`](https://github.com/JuliaLang/julia/blob/90338777498b9af67eeb1fd56553dea3a2124cae/base/iterators.jl#L1234) has some tricks to figure this out.

---

<div class="post-metadata">

**Author:** ![Olof\_Salberger](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/olof_salberger/32/4850_2.png) [@Olof\_Salberger](https://discourse.julialang.org/u/Olof_Salberger)\
**Post date:** [April 13, 2021, 5:57pm UTC](https://discourse.julialang.org/t/obtaining-the-type-of-the-state-object-returned-by-iterate/59190/3 "2021-04-13T17:57:39Z")

</div>

That was very useful, thank you!

---

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [December 17, 2022, 11:28am UTC](https://discourse.julialang.org/t/obtaining-the-type-of-the-state-object-returned-by-iterate/59190/4 "2022-12-17T11:28:32Z")

</div>

Glancing at the `Base.Iterators.Stateful` code, it seems the relevant functionality relies on internal Julia functions? Is there perhaps a better way for code that’s not a part of the Julia project?

---

<div class="post-metadata">

**Author:** ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)\
**Post date:** [December 17, 2022, 12:15pm UTC](https://discourse.julialang.org/t/obtaining-the-type-of-the-state-object-returned-by-iterate/59190/5 "2022-12-17T12:15:35Z")

</div>

There’s no documented official way to get the “iterator eltype”. Possible solutions rely on type inference, and basically boil down to `Core.Compiler.return_type(Base._iterator_upper_bound, Tuple{T})`.
