`prod` over empty collection with known eltype gives "reducing over empty collection ..."

Compare:

julia> typeof(a for a in Float64[])
Base.Generator{Vector{Float64}, typeof(identity)}

julia> typeof(a for (a, b) in Tuple{Float64, Float64}[])
Base.Generator{Vector{Tuple{Float64, Float64}}, var"#5#6"}

As you can see, the simpler iterator is a generator parameterized by the named function identity, while the more complex one is parameterized by an anonymous function. Presumably identity is special-cased in the implementation of prod, or one of prod’s callees. So I don’t think there’s any runtime use of type inference here.

If you’re wondering why is identity even involved here, the answer is that (a for a in Float64[]) is, at an early stage, the “lowering”, transformed into something like Iterators.map(identity, Float64[]). Check yourself with Meta.@lower (a for a in Float64[]), although keep in mind the output mentions internals. So basically Julia recognizes a for a as the identity function, so it just uses identity, instead of a new anonymous function.

Yeah, already said as much in my previous message.

1 Like