Obtaining the type of the state object returned by Iterate

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.

1 Like

Base.Iterators.Stateful has some tricks to figure this out.

1 Like

That was very useful, thank you!