The following snippet of code unexpectedly returns Any
and not Int64
from eltype
:
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.
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> 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.