It would be nice with something like a "type tagged union", i.e.,
message Animal {
union Inner {
message Dog dog = 1;
message Cat cat = 2;
}
optional double weight = 1;
required Inner animal = 2;
}
message AnimalList {
repeated Animal animals = 1;
}
It is a quite common use case to want to have one of several alternative
structures/messages and this extension would allow heterogeneous
sequences to be represented entirely in protobuf.
Best wishes,
Mats Kindahl
--
Mats Kindahl
Lead Software Developer
Replication Team
MySQL AB, www.mysql.com
The problem I see with this example is that you have to know all the derived types at compile time.
Is there any pattern for an extensible model, where you could add more derived types without touching the base type?
It would be nice to extend the syntax for inheritance, and use a form
of annotation to give the compiler hint as to how the classes /
interfaces should be generated....
The problem I see with this example is that you have to know all the derived types at compile time.No you don't. The Cat and Dog types could be declared in a completely separate .proto file. They could optionally not be compiled in to programs which do not use them.
Hmm, in the example, since the exact Animal is not known during
compile time, this information needs to be sent over the wire to the
parser as part of the encoded data -- perhaps the available wire type
can be extended to send the runtime type information over the wire
(e.g. using type 3 or 4 which has been deprecated to indicate a length-
limited type used to send the name of the proto-buffer message name to
use for the parsing...)?
Only the type Cat needs to be sent over the wire.. the hierachi
information was known during compile time....
If there are thousands of possible Animals, don't you have no choice
but to compile all the possible Animals since the true impl is
determined dynamically...?
However, if both the sender and the
receiver agree to only deal with a limited subset of all possible
cases, they can always include only those subsets necessary for the
application - since the other types will never be sent; else error/
exceptions will be thrown.
FWIW-- I'm just beginning to try in earnest to figure what to do for
the C binding -- since the exact type of the message isn't included in
the message itself, it falls on the code to pick the exact type for
the message.
Going back to my logging use case, if all my
log messages have a 'required string transactionId=1' field, then it
would be nice if I could just:
message Transacted {
required string transactionId=1;
extensions 100 to max;
}
message Foo extends Transacted { ... }
message Bar extends Transacted { ... }
message Baz extends Transacted { ... }
Now if the language supports inheritance, sure why not have the Foo
class extends Transacted. But even if the language does not support
inheritance, I think you still get a win because you just made a Foo
class with all the fields that Transacted has without having to repeat
them (and possibly get them wrong) in the proto definition.