Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Protobuf: enum with default value

In google's Protocol Buffers, I use large enums and I have to assign each integer value explicitly:

  enum Function {ProcessLibrary=0;
                 RotateLeft=1;
                 RotateRight=2;
                 ...}

This is very annoying and ugly. Is there a way to avoid these integer values in the code? something like:

  enum Function {ProcessLibrary;
                 RotateLeft;
                 RotateRight;
                 ...}
like image 252
zvisofer Avatar asked Mar 20 '26 09:03

zvisofer


1 Answers

No, basically. This is deliberate to prevent huge errors when adding / removing enums, and to allow for non-contiguous enums.

In most real-world cases where the list of names is already defined elsewhere, you can write a 5 line script to add =n onto each - heck, a spreadsheet calculation and "fill down" would go a long way to it - paste names into the first column, copy the generated lines out of the second.

like image 150
Marc Gravell Avatar answered Mar 23 '26 22:03

Marc Gravell