Why are there no custom default values in proto3?

Daniel Pauli picture Daniel Pauli · Oct 19, 2015 · Viewed 25.6k times · Source

The proto2 version of Protocol Buffers allows to specify default values for message elements:

optional double scaling_factor = 3 [default = 1.0];

Why is this no longer possible in proto3? I consider this a neat feature to save additional bytes on the wire without the need of writing any wrapper code.

Answer

Kenton Varda picture Kenton Varda · Oct 20, 2015

My understanding is that proto3 no longer allows you to detect field presence and no longer supports non-zero default values because this makes it easier to implement protobufs in terms of "plain old structs" in various languages, without the need to generate accessor methods. This is perceived as making Protobuf easier to use in those languages.

(I personally think that languages which lack accessors and properties aren't very good languages and protobuf should not design down to them, but it's not my project anymore.)