# Testing return types of functions

**URL:** <https://discourse.julialang.org/t/testing-return-types-of-functions/50049>\
**Category:** General Usage\
**Created:** [November 12, 2020, 1:41pm UTC](https://discourse.julialang.org/t/testing-return-types-of-functions/50049 "2020-11-12T13:41:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![wulpuqu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wulpuqu/32/17640_2.png) [@wulpuqu](https://discourse.julialang.org/u/wulpuqu)\
**Post date:** [November 12, 2020, 1:41pm UTC](https://discourse.julialang.org/t/testing-return-types-of-functions/50049/1 "2020-11-12T13:41:17Z")

</div>

Hello everyone,

Given a function definition like

```julia
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:

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

```

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

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [November 12, 2020, 2:00pm UTC](https://discourse.julialang.org/t/testing-return-types-of-functions/50049/2 "2020-11-12T14:00:33Z")

</div>

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
julia> Base.return_types(+, (Int, Float64))
1-element Array{Any,1}:
 Float64

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

```

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

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

```

---

<div class="post-metadata">

**Author:** ![wulpuqu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wulpuqu/32/17640_2.png) [@wulpuqu](https://discourse.julialang.org/u/wulpuqu)\
**Post date:** [November 12, 2020, 2:41pm UTC](https://discourse.julialang.org/t/testing-return-types-of-functions/50049/3 "2020-11-12T14:41:41Z")

</div>

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.
