# Iterators.product is not type stable?

**URL:** <https://discourse.julialang.org/t/iterators-product-is-not-type-stable/14481>\
**Category:** General Usage\
**Tags:** type-stability, iterators, cartesianindices\
**Created:** [September 3, 2018, 1:43pm UTC](https://discourse.julialang.org/t/iterators-product-is-not-type-stable/14481 "2018-09-03T13:43:47Z")\
**Posts on this page:** 1\
**Page:** 2

<div class="post-metadata">

**Author:** ![Jean\_Michel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jean_michel/32/8282_2.png) [@Jean\_Michel](https://discourse.julialang.org/u/Jean_Michel)\
**Post date:** [September 9, 2018, 5:28pm UTC](https://discourse.julialang.org/t/iterators-product-is-not-type-stable/14481/21 "2018-09-09T17:28:59Z")

</div>

Here is my current conclusions:

```julia
function cartesian(l::Vararg{AbstractVector{T}})where T
  v=vec(collect(Iterators.product(l...)))
end

f(n)=cartesian([1:i for i in 2:rand(n:n)]...)

function cartesian1(l::Vararg{AbstractVector{T}})::Matrix{T} where T
  v=vec(collect(Iterators.product(l...)))
  [y[i] for y in v,i in 1:length(l)]
end

g(n)=cartesian1([1:i for i in 2:rand(n:n)]...)

```

In the above `cartesian` is the fastest. However f is not-stable because I do not know how to describe  
the output type of `cartesian`. On the other hand, g is type-stable because I can describe the type of `cartesian1`. Thus even though f is faster than g, amidst a longer piece of code g performs better.  
cartesian1 is not as fast as cartesian, but would be if I could transform more efficiently v in an array.  
Any thoughts on that?

[Previous page](https://discourse.julialang.org/t/iterators-product-is-not-type-stable/14481.md?page=1)
