constants, enums, signed vs unsigned

2,818 views
Skip to first unread message

maxw

unread,
Feb 14, 2011, 12:15:02 PM2/14/11
to Protocol Buffers
Hello,

I didn't find crystal-clear confirmation in the documentation, but it
looks like enum values are considered _signed_ 32-bit integers. When
I try to assign 4294967295 to an enum value in a .proto file, protoc
complains. Is there any way to have unsigned 32-bit integer
constants?

I did find on this forum a suggestion ("Issue 60"), which basically
declares a "Constants" message, with fields that have default values -
those default values can then be retrieved. This does work, and also
has the benefit of allowing more types of constants (than just 32-bit
integers).

The dilemma that I'm facing is what should be the guideline to choose
between enum and this default value trick? Would it be considered
"bad form" to mix both styles?

Thank you!

Kenton Varda

unread,
Feb 22, 2011, 1:27:14 PM2/22/11
to maxw, Protocol Buffers
Enums are signed, yes.

I would suggest not defining constants in .proto files at all.  There isn't really any good way of doing it.  Enums really aren't designed to be used as numeric constants (especially in Java where they aren't even numeric), but default values are also a pretty awkward way to do it.


--
You received this message because you are subscribed to the Google Groups "Protocol Buffers" group.
To post to this group, send email to prot...@googlegroups.com.
To unsubscribe from this group, send email to protobuf+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/protobuf?hl=en.


Ben Wright

unread,
Feb 22, 2011, 3:46:27 PM2/22/11
to Protocol Buffers
Seems an odd use case to capture a constant in a proto file... but my
suggestion would be to extend FileOptions or MessageOptions and
provide options that hold your constant values.
Reply all
Reply to author
Forward
0 new messages