Any function return inference in Julia >1.3?

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

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

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

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.)

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.

1 Like

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