Testing return types of functions

Hello everyone,

Given a function definition like

g(varargs...)::T2 = ...

Is there a way to retrieve the return type T2 without evaluating typeof(g(varargs...)) ?

I would like to test if a function is returning an aggregate data type like an AbstractArray, AbstractDict or a struct without testing the output.

I thought of something like this for testing if the returned type is an aggregate type:

isaggregate(f::Type) = ~isempty(methods(getindex,(T,Any))) || ~isempty(methods(getproperty,(T,Any)))

More generally, is there a good way of doing this ?

If you mean testing as in unit testing, Test offers the @inferred macro. If you mean testing as in checking what the return type will be before running g, you can also do this, but not through any exported methods (so beware).
You can use Base._return_type and Base.return_types

julia> Base.return_types(+, (Int, Float64))
1-element Array{Any,1}:

julia> Base._return_type(+, (Int, Float64))

The return type is only as stable as your functions and inputs though:

julia> Base.return_types(+, (Complex, Int))
2-element Array{Any,1}:
 Complex{_A} where _A

Thank you for your help !

I need to check the returned type of functions when parsing expressions. I guess I could also use code_typed for doing this (right now I don’t really want to handle two representations: Typed CodeInfo and Expressions ).

In comparison with Base.return_types, It seems that Base._return_type outputs a more general but not too large type. So I guess, Base._return_type is the way to go.