# Any function return inference in Julia \>1.3?

**URL:** https://discourse.julialang.org/t/any-function-return-inference-in-julia-1-3/43290
**Category:** New to Julia
**Created:** [July 18, 2020, 3:31pm UTC](https://discourse.julialang.org/t/any-function-return-inference-in-julia-1-3/43290 "2020-07-18T15:31:49Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![tro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tro3/32/12355_2.png) [@tro3](https://discourse.julialang.org/u/tro3)
#### Post date: [July 18, 2020, 3:31pm UTC](https://discourse.julialang.org/t/any-function-return-inference-in-julia-1-3/43290/1 "2020-07-18T15:31:49Z")

</div>

Has there been any addition in Julia 1.4/1.5 of function return inference, given arg types? As of Julia 1.3, I was using

```julia
_detect_type(fn, itr) = eltype(map(fn, empty(itr)))

function myfunc(fn, itr)
  result = Vector{_detect_type(fn, itr)}(undef, N)
  ...
end

```

to pre-allocate memory in such a way that the inference engine would keep `myfunc` type-stable. But this method breaks down on anything without an `empty` function (eg a Generator of an arbitrary struct).

That `_detect_type` was always a hack. Has a method to infer function return types at compile time been added recently, by any chance? Just hoping…

(PS. I wonder if I am doing something in a non-Julian fashion that this concept does not seem to have been a priority.)

---

<div class="post-metadata">

### Author: ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)
#### Post date: [July 18, 2020, 3:54pm UTC](https://discourse.julialang.org/t/any-function-return-inference-in-julia-1-3/43290/2 "2020-07-18T15:54:55Z")

</div>

> [@tro3](#):
>
> `_detect_type(fn, itr) = eltype(map(fn, empty(itr)))`

I think this ends up calling `Core.Compiler.return_type(fn, Tuple{eltype(itr)})` on the inside. Which is not new, but comes with various brightly coloured warning stickers notifying you that you cannot sue anyone if it decides to give you `Any` one day.

But to allow generators you’ll need something else anyway, since e.g. `eltype(i for i in 1:3) == Any`. I guess the most Julian thing would be to evaluate the first element, and widen later if subsequent ones differ.

---

<div class="post-metadata">

### Author: ![tro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tro3/32/12355_2.png) [@tro3](https://discourse.julialang.org/u/tro3)
#### Post date: [July 18, 2020, 4:00pm UTC](https://discourse.julialang.org/t/any-function-return-inference-in-julia-1-3/43290/3 "2020-07-18T16:00:26Z")

</div>

Thanks. May not handle generators, but it will allow multi-dim arrays. Much appreciated.
