0

Need to understand the below definition, from the protobuf tutorial:

mergeFrom(Message other): (builder only) merges the contents of other into this message, overwriting singular scalar fields, merging composite fields, and concatenating repeated fields.

Based on the answers found here, I could assume that:

Question 1:

  • A. String is a scalar field, since it's not an array.

OR

  • B. String is-not a scalar field, since it's (like) an array of chars!

So, I would want to have clarity on what are the EXACT scalar and composite fields in JAVA? And why?

Question 2:

I assume Object to be of 'composite type'. If that's the case, how would objects be merged, in Protobuf?

If Object is not a 'composite type', why?, then?

1

The terms 'scalar type' or 'scalar field' are usually used to contrast them with compound types/fields. A compound type is easiest to define and it is a type that contains multiple distinct elements. These elements can have the same type, in which case the compound is an array or a list, or different types, in which case the compound is typically a struct or a class. A scalar type is a type that contains only a single element.

The set of scalar types contains at least the primitive types, but it might also contain String types, depending on how the community looks at those types. Typically, if a language (or specification like Protocol Buffers) contains a distinct type for storing string data, as opposed to storing strings as an array of characters, then those string types are considered to be scalar types.


To understand the mergeFrom function, you don't need to know what Java considers to be scalars or compound fields, but what Protocol Buffers considers to be scalar or compound.

The list of scalar types in Protocol Buffers is listed here. It consists of primitive types (floating point, integers, booleans, etc.) and string types.
Compound types correspond to the (user-defined) Messages within Protocol Buffers.
Enumeration types may be an odd-one out here. They are formally documented as compound types, but my guess is that the merging semantics are likely to be closer to that of scalar types.

As the java.Object class does not have a meaning within the context of Protocol Buffers, it also makes no sense of talking about merging it. The behaviour of the function also appears to only be defined when merging two messages of the same type.

  • Your statement "The list of scalar types in Protocol Buffers is listed here. It consists of primitive types (floating point, integers, booleans, etc.), string and enumeration types." - means that, enumerations - is of 'scalar type'. But, based on the protobuf documentation you've provided, "you can also specify composite types for your fields, including enumerations and other message types.", enumerations - is of 'composite types'. You may want to edit your answer, accordingly. – RafiAlhamd Jul 11 at 10:10
  • @RafiAlhamd: I updated my answer. I initially characterized enumerations as scalars based on their underlying implementation details and how they are likely to be merged. Made that more explicit now. – Bart van Ingen Schenau Jul 11 at 10:43
2

So, I would want to have clarity on what are the EXACT scalar and composite fields in JAVA? And why?

It's strictly to do with the types defined in a .proto file. Java the language doesn't define the meaning here.

Anything you have defined an enum or message for is composite. The built-in types are scalar.

Elements that are defined required or optional are singular, those that are repeated are repeated.

I assume Object to be of 'composite type'. If that's the case, how would objects be merged, in Protobuf?

java.lang.Object isn't a type in Protobuf. It isn't present to be merged.

If Object is not a 'composite type', why?, then?

Protobuf can only operate on things it has descriptions for. Remember there is a .proto -> .java compile step.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.