These are tough problems. In another context I've started wondering whether
there might be a need to develop a package that provides a convenient hack
around type-instability. I have no idea whether this will actually be useful
in practice, but the idea might be something like the following: suppose you
have 3 different types, A, B, and C. We can create a type-stable object
container like this:
immutable ObjectContainer
objtype::DataType
objdata::Vector{UInt8}
end
where objdata is a wrapper around the memory associated with the object. Then
one creates a @dispatch_for macro,
@dispatch_for A B C myfunction(obj::ObjectContainer, args...)
which generates code that looks like this:
function myfunction(obj::ObjectContainer, ...)
if obj.objtype == A
myfunction(unsafe_pointer_to_objref(convert(Ptr{A},
pointer(obj.objdata)), args...)
elseif obj.objtype == B
...
end
Basically the idea here is that you bypass generic runtime method lookup and
handle dispatch manually. Naturally, you have to write myfunction methods for
A, B, and C as usual. There's a remote chance you'd have to give these
different names (myfunction_A, myfunction_B, myfunction_C) and have the
@dispatch_for macro use these names, but I hope that wouldn't be necessary.
You'd also want to declare all of them @noinline.
There are quite a few issues to investigate: will the array-wrapper allocation
destroy any benefit? Can one realistically handle multiple dispatch, not just
single dispatch on the first argument?
--Tim