Iterators.jl icon indicating copy to clipboard operation
Iterators.jl copied to clipboard

Array{Any} when collecting chained generators

Open CarloLucibello opened this issue 9 years ago • 7 comments

Collecting a generator works as expected

julia> collect(i for i=1:10)
10-element Array{Int64,1}:
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10

But if you chain it

julia> collect(chain(i for i=1:10))
10-element Array{Any,1}:
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10

What puzzles me is that eltype(i for i=1:10) == Any

CarloLucibello avatar Oct 30 '16 09:10 CarloLucibello

eltype(i for i=1:10) == Any is an issue, I think. But probably an issue for JuliaLang/julia. I figure there could be two solutions:

  1. Something involving Base.return_types to do inference on the generator function in the general case

OR

  1. Special case a generator which just returns the results of an iterator (I think this would need some parser attention, i.e., have the parser use identity instead of (x)->x so that we can dispatch on it after.

iamed2 avatar Oct 30 '16 13:10 iamed2

eltype(i for i=1:10) == Any is an issue,

It's not an issue.

  1. Something involving Base.return_types to do inference on the generator function in the general case

And we shouldn't do this.

I think we just shouldn't rely on eltype in collect.

yuyichao avatar Oct 30 '16 13:10 yuyichao

I wasn't sure whether Base.return_types was fair game or not.

I was thinking up some @generated solution until I realized that I can tell collect to not rely on eltype fairly simply. This line solves things:

Base.iteratoreltype(c::Chain) = EltypeUnknown()

but we may not need it with #85 (reviewing performance for that now). We will.

iamed2 avatar Oct 30 '16 18:10 iamed2

fixed by #85

CarloLucibello avatar Jan 21 '17 11:01 CarloLucibello

No, it wasn't

iamed2 avatar Jan 21 '17 14:01 iamed2

ouch, sorry, did the wrong test, ranges instead of iterators

CarloLucibello avatar Jan 21 '17 21:01 CarloLucibello

That's okay :)

iamed2 avatar Jan 21 '17 21:01 iamed2