Polymorphism
The idea of this section is to explain how inheritance may be implemented in order to enable polymorphism among messages. There are two methods, depending on the proto version that we are using:
- Proto 2 uses Extensions
- Proto 3 deprecates Extensions in favor of Any statement
Extensions are a range of tags reserved for future extended messages so you can use them for different purposes:
syntax
=
"proto2"
;
message
Film
{
extensions
50
to
99
;
}
message
TerrorFilm
{
extend
Film
{
required
TerrorFilm
film
=
50
;
}
bool
has_zombies
=
1
;
}
message
ScienceFictionFilm
{
extend
Film
{
required
ScienceFictionFilm
film
=
51
;
}
bool
has_jedis
=
1
;
}
In terms of binary serialization the final message will look the same as a non extended message, but in terms of code generated for accessing those fields, it’s a different story. Here is an example for setting the extended field film
in a Java implementation.
ScienceFictionFilm...