Hacker News new | past | comments | ask | show | jobs | submit login

> Fields with scalar types are always present. Even if you don’t set them. Did I mention that (at least in proto31) all protobuffers can be zero-initialized with absolutely no data in them? Scalar fields get false-y values—uint32 is initialized to 0 for example, and string is initialized as "".

> It’s impossible to differentiate a field that was missing in a protobuffer from one that was assigned to the default value. Presumably this decision is in place in order to allow for an optimization of not needing to send default scalar values over the wire.

I believe there’s a trick you can do if you mark it as a “oneof” with only one field.




> It’s impossible to differentiate a field that was missing in a protobuffer from one that was assigned to the default value. Presumably this decision is in place in order to allow for an optimization of not needing to send default scalar values over the wire.

Isn't this just flat incorrect? You can tell the difference between set-to-default and not-set with buffer.has_some_field().


Not for things like ints and strings.


It depends on which version you're using.

In proto1 and proto2, every field had a "has" method and "explicitly set to default value" was different from "absent".

In proto3, they tried to remove this feature, and instead said that for basic types, "set to default" and "absent" are the same thing.

(I wrote proto2. I left Google before proto3 came about.)


Thanks. I must be only used to proto2.




Consider applying for YC's Summer 2025 batch! Applications are open till May 13

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: