Other than @forward
I can only think of the obvious solutions:
One is
function get_wheel_radius(x :: T) where T
if hasfield(T, :wheel) && isa(getfield(x, :wheel), Wheel)
return get_radius(x.wheel)
else
error("$T has no wheels")
end
end
This hard wires field names (not good). One could probably cook up a loop that defines accessor functions for a list of fields without having to repeat the same code over and over again for each field. I doubt that is even less code than @forward
.
Another option might be traits. But defining a hasWheel
trait for each object also doesn’t look better than @forward
.