# Generator - iterators should deliver accurate eltype

**URL:** https://discourse.julialang.org/t/generator-iterators-should-deliver-accurate-eltype/5149
**Category:** Internals & Design
**Created:** [July 31, 2017, 10:28am UTC](https://discourse.julialang.org/t/generator-iterators-should-deliver-accurate-eltype/5149 "2017-07-31T10:28:40Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![klacru](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/klacru/32/27890_2.png) [@klacru](https://discourse.julialang.org/u/klacru)
#### Post date: [July 31, 2017, 10:28am UTC](https://discourse.julialang.org/t/generator-iterators-should-deliver-accurate-eltype/5149/1 "2017-07-31T10:28:40Z")

</div>

The following snippet of code unexpectedly returns `Any` and not `Int64` from `eltype`:

```julia-repl
julia> g = (x for x in 1:5)
Base.Generator{UnitRange{Int64},##1#2}(#1, 1:5)
julia> eltype(g)
Any

```

Reason seems that `Base.Generator` does not support `eltype` nor `Base.iteratoreltype`.

I want to propose the following additions to `generator.jl` to fix that.

```julia
Base.iteratoreltype(g::Base.Generator{I,F}) where {I,F} = Base.iteratoreltype(g.iter)
Base.eltype(g::Base.Generator{I,F}) where {I,F} = promote_type(Base.return_types(g.f, (eltype(g.iter),))...)
Base.eltype(g::Base.Generator{I,typeof(identity)}) where I = eltype(g.iter) # won't work for example

```

After that I receive:

```julia-repl
julia> g = (x for x in 1:5 if x != 3);

julia> h = (x for x in 0.5:0.1:11.0)
Base.Generator{StepRangeLen{Float64,Base.TwicePrecision{Float64},Base.TwicePrecision{Float64}},##31#32}(#31, 0.5:0.1:11.0)
julia> i = (exp(x) for x in -1:2:10)
Base.Generator{StepRange{Int64,Int64},Base.#exp}(exp, -1:2:9)
julia> eltype(g)
Int64
julia> eltype(h)
Float64
julia> eltype(i)
Float64

```

All fine… I propose to discuss the addition of the 3 lines of code.

---

<div class="post-metadata">

### Author: ![bjarthur](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bjarthur/32/9638_2.png) [@bjarthur](https://discourse.julialang.org/u/bjarthur)
#### Post date: [August 3, 2017, 11:39am UTC](https://discourse.julialang.org/t/generator-iterators-should-deliver-accurate-eltype/5149/2 "2017-08-03T11:39:57Z")

</div>

might try submitting a PR on github

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [August 3, 2017, 1:11pm UTC](https://discourse.julialang.org/t/generator-iterators-should-deliver-accurate-eltype/5149/3 "2017-08-03T13:11:26Z")

</div>

See e.g. [https://github.com/JuliaLang/julia/issues/18695](https://github.com/JuliaLang/julia/issues/18695) and links therein.

---

<div class="post-metadata">

### Author: ![klacru](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/klacru/32/27890_2.png) [@klacru](https://discourse.julialang.org/u/klacru)
#### Post date: [August 3, 2017, 1:27pm UTC](https://discourse.julialang.org/t/generator-iterators-should-deliver-accurate-eltype/5149/4 "2017-08-03T13:27:36Z")

</div>

@stevengj I see, that the problem is not new, but has not been solved, because there is no _general_ solution, or for other reasons, I could not recognize in the referred text.  
But if the Generator delivers an element type, we should use that, I think. If not, we could throw an appropriate exception.  
In my proposal, the `eltype` of `g::Generator` is `delegated to eltype(g.f)`, which also works recursively, as the examples show.  
The third line does not work, because for `g = (x for x in ...)`, the compiler does not generate `g.f == identity`, as I had assumed.  
In the second line, `promote_type(Base.return_types(g.f, (eltype(g.iter),))...)` could probably replaced by `Core.Inference.return_type(g.f, (eltype(g.iter),))`.
