Question

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;
                 ...}
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top