Comment #3 on issue 312 by [email protected]: Required field with default
value serializes if not set
http://code.google.com/p/protobuf/issues/detail?id=312
The bug you are reporting here actually has nothing to do with default
values. You'd see the exact same behavior even if you had set no default
value.
The relationship between required fields and default values is somewhat
confusing. In fact, there is no relationship at all, even though it seems
like they would be related. The default value simply defines what is
returned by the field's getter if no value has explicitly been set. Since
required fields must be explicitly set, it's generally not very useful to
define a default value for a required field. BTW, all fields have default
values -- if you don't define one explicitly, the default value is
zero/empty.
Back to the bug here. The problem is, IsInitialized() is not free, so we
do not want to force it to be called every time a message is serialized
when 99.999% of the time it will pass. The only time the initialization
check should ever fail is when the calling code is incorrect, which ideally
should only happen in development, and should be caught in testing /
debugging. Hence, we skip the check in production.
As Jisi points out, this doesn't work very well if you are doing your
development against a protobuf library which is itself compiled with
-DNDEBUG, which is the default if you build it with configure/make. You
can compile a debug build of protobufs by overriding CXXFLAGS to NOT
include -DNDEBUG, for example:
./configure CXXFLAGS='-O2 -g'
make
I recommend compiling your debug builds and tests against such a debug
build of libprotobuf.
--
You received this message because you are subscribed to the Google Groups "Protocol
Buffers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/protobuf?hl=en.