There seem to be 2 techniques to effect pseudo-inheritance/polymorphism.
oneof:
message Animal {
string name = 1;
oneof animal_type {
Bird bird = 2;
Dog dog = 3;
}
}
message Bird {
bool can_fly = 1;
}
message Dog {
string breed = 1;
}
mixin common fields:
message Animal {
string name = 1;
}
message Bird {
Animal animal = 1;
bool can_fly = 2;
}
message Dog {
Animal animal = 1;
string breed = 2;
}
Using oneof makes it easier to define a generic collection of Animals, though all the subtypes need to be defined by the author of Animal.
Using the mixin strategy makes it easier for any "subclass" to extend the base type.
I'm trying to understand all the tradeoffs and am curious if people have developed best practices on this. Are there other considerations?
jeff