I am curious about a usecase along the following lines, focused on still writing code as if we are using typical multiple dispatch from Julia:
Someone already has a simple ray-tracing library written in Julia which implements tens of different solid object primitives (each a separate struct with supertype Abstract3DObject). Then a render function looks something like
function render(scene::Vector{Abstract3DObject})
for object in scene
find_ray(object)
...
end
find_ray itself has a ton of methods for the various object types.
My understanding is that this is a rather bad way to do things in Julia because
- one usually has a ton of different types stored in
sceneso the vector contains boxed objects even if all the objects are otherwise pretty simple -
find_rayinside ofrenderwill be using slower dynamical dispatch because there are too many types for automatic union splitting
Can SumTypes be used here without rewriting the library to make Abstract3DObject a sumtype? Can the sumtype be created dynamically by some wrapper for the render function, so that everything except render can be written as if SumType is not involved?
I guess I am asking for a function that dynamically creates a sumtype when one of its arguments is a Vector of many structs of the same abstract type.