In these cases, I usually prefer to compose my types.
Here is an example of what I mean:
abstract A type B <: A x y z end type C <: A b::B w end
Note that C is still a subtype of A , but contains an instance of B as a field.
Update
It is true that you can no longer access cx , but you need to make cbx . There is an easy way to do this.
Suppose you have a function
function my_func(a::A)
If my_func should work with any subtype of A , it can only access fields that are common to all subtypes of A In this case, these are x , y and z .
Knowing this, I would also define a specialized version of this method for sending C instances as follows:
my_func(c::C) = my_func(cb)
This is a bit more verbose, but you can easily wrap all of these functions in metaprogramming for a loop that @eval uses and generates them all at once. See docs for more details.
source share