Base.return_types function

I have two questions about the following experiment:

  1. Why are there so many more possibilities with AbstractArray? Is it correct?
  2. Why doesn’t it return unique results? Maybe it should.
julia> Base.return_types(iterate, Tuple{AbstractArray{Int,1}})
11-element Array{Any,1}:
 Union{Nothing, Tuple{Any,Int64}}                                        
 Union{Nothing, Tuple{Any,Any}}                                          
 Union{Nothing, Tuple{Any,Any}}                                          
 Union{Nothing, Tuple{Any,Any}}                                          
 Any                                                                     
 Any                                                                     
 Union{Nothing, Tuple{Int64,Int64}}                                      
 Union{Nothing, Tuple{Any,Int64}}                                        
 Union{Nothing, Tuple{Int64,Tuple{Int64,UInt64}}}                        
 Union{Nothing, Tuple{Any,Union{Tuple{Int64,Any}, Tuple{Int64,Any,Any}}}}
 Any                                                                     

julia> Base.return_types(iterate, Tuple{Array{Int,1}}) |> unique
1-element Array{Any,1}:
 Union{Nothing, Tuple{Int64,Int64}}

Generally an AbstractArray could be a lot of things (eg linear or cartesian indexing, various implementations), so I am not sure the compiler could tell you much more. Using Base.return_types is generally discouraged even for concrete types, but for abstract ones it is even less meaningful.

I am not sure why you think Base.return_types should return a single-element vector in this case.

Please let me elaborate. The interface for iterate requires that the function returns either nothing or a tuple of (value, state). Without knowing how the concrete types are implemented, I still expect the type of the next value to be an Int. Hence Union{Nothing, Tuple{Int64,anything}} makes sense to me. But I can’t see how Union{Nothing, Tuple{Any,Any}} satisfies the interface contract. Likewise for others.

Sorry, I pasted in the wrong example in the second case. I meant to ask why the first case returned 11 elements rather than 6.

julia> Base.return_types(iterate, Tuple{AbstractArray{Int,1}}) |> unique
6-element Array{Any,1}:
 Union{Nothing, Tuple{Any,Int64}}                                        
 Union{Nothing, Tuple{Any,Any}}                                          
 Any                                                                     
 Union{Nothing, Tuple{Int64,Int64}}                                      
 Union{Nothing, Tuple{Int64,Tuple{Int64,UInt64}}}                        
 Union{Nothing, Tuple{Any,Union{Tuple{Int64,Any}, Tuple{Int64,Any,Any}}}}

Base.return_types is not guaranteed to return the narrowest possible types.

Since Int <: Any, this is valid.

4 Likes