Ho can you dispatch on Iterator eltype including generators? I ask in an attempt to fix the following interface problem.
Right now I am working on the get function of an interface that has many different types to dispatch, and potentially a lot of over head for each get initially. Something like:
struct InterfaceIndirect index::Int64 end
struct ApplicationIndirect ref::InterfaceIndirect end
function interface_get(indirect::InterfaceIndirect)
#lots of prework which makes it impractical to broadcast to this function
prework=rand(100_000_000)
return prework[indirect.index]
end
function interface_get(indirect::AnotherInterfaceIndirect)
#same kind of thing as above but different
end
function interface_get(v_indirect::Vector{InterfaceIndirect})
#lots of prework
prework=rand(100_000_000)
return [ prework[indirect.index] for indirect in v_ind ]
end
function interface_get(v_indirect::Vector{AnotherInterfaceIndirect})
#ditto
end
function application_get(app_indirect::ApplicationIndirect)
interface_get(app_indirect.ref)
end
function application_get(v_app_indirect::Vector{ApplicationIndirect})
###This temporary array sucks####
v_indirect = [app_indirect.ref for app_indirect in v_app_indirect]
interface_get(v_indirect)
end
The temporary array for the application vector indirection seems sub-optimal
Now with that iteration and generators are so fast it would be great if I could instead to something like:
interface_get(iter::IT) where IT = _interface_get(eltype(IT),iter)
function _interface_get(::Type{T}, iter) where T <: InterfaceIndirect
#lots of prework
prework=rand(100_000_000)
return [ prework[indirect.index] for indirect in iter ]
end
function application_get(v_app_indirect::Vector{ApplicationIndirect})
#No more temporary array
interface_get(app_indirect.ref for app_indirect in v_app_indirect)
end
But unfortunately Generators do not have an eltype, so that implementation can not be used.
As I recently inquired about it:
So I ask what is the implementation pattern that gets around this? Or is there an alternative way to achieve this.