Type stability problem (Vcat, Tuple and function)

I think we are approaching a point where does not make sense to try to achieve type-stability.

qux(x) = Tuple(reduce(vcat,y) for y in x)

If different values of x (not types) change the type of the return (i.e., the amount of elements in x will determine the type of the tuple returned), then type-instability is inherent and impossible to avoid. What you can do is minimize damage by either:

  1. Putting a type annotation by the side of each call to qux (this only works if you are always working with vectors of the same size and type and therefore you know which will be the type returned by the function), e.g., qux(my_known_length_and_type_vector) :: Tuple{Int, Int}.
  2. Let the function return a type-unstable value, and then, immediately pass this value as an argument to another function (that does everything you could want to do with this value). This way, for each possible type returned by the type-unstable function, this second function will compile a specialized version and the type-instability will be restricted to dynamic dispatch of this single secondary function (instead of a dynamic dispatch for every subsequent call that takes the variable of unstable type).
3 Likes