문제

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;
                 ...}
도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top