28

I have the following .proto file :

enum Enum1{
    X=0;
    Y=1;    
}
message SomeClass{
    required  Enum1 enum1=1;
    required Enum2 enum2=2;
}
enum Enum2{
    X=0;
    Z=1;    
}

When I try to comile it using protoc , I get the following error :

proto.proto:19:5: "X" is already defined proto.proto:19:5: Note that enum values use C++ scoping rules, meaning that enum values are siblings of their type, not children of it. Therefore, "X" must be unique , not just within "Enum2".

I there any way I could overcome this issue !

2
  • You mean rename X @ Enum2 !! if ys , I cann't right now as there dependency over it on the application, Commented Oct 31, 2011 at 11:09
  • You have two same enum instances, X. Commented Jul 7, 2015 at 8:46

3 Answers 3

42
+50

You could include your enum inside another message so the visibility will not conflict.

Exemple :

message Enum1{
 enum Enum{
     X=0;
     Y=1;    
 }
}

message Enum2{
 enum Enum{
     X=0;
     Y=1;    
 }
}

message SomeClass{
    required  Enum1.Enum enum1=1;
    required Enum2.Enum enum2=0;
}

You could also prefix your enum value with something. If you don't change the number after the name of your value, it should stay compatible with your old version : ex:

enum Enum1{
    E1_X=0;
    E1_Y=1;    
}
enum Enum2{
    E2_X=0;
    E2_Z=1;    
}
message SomeClass{
    required  Enum1 enum1=1;
    required Enum2 enum2=2;
}
Sign up to request clarification or add additional context in comments.

2 Comments

required is removed from proto3
This question is 8 years old. It would work with proto3 if used without required.
1

You can use prefix in enumeration then

enum Enum1{
    E1_X=0;
    E1_Y=1;    
}
message SomeClass{
    required Enum1 enum1=1;
    required Enum2 enum2=2;
}
enum Enum2{
    E2_X=0;
    E2_Z=1;    
}

Comments

0

instead of

message SomeClass{
required  Enum1 enum1=1;
required Enum2 enum2=2; }

i think you should have something like

message SomeClass{
    required  Enum1 enum1.Y;
    required Enum2 enum2.Z;
}

edit: you tagged this as Java, but in the question you refer to c++, witch one it is?

edit2: After googling a bit I found this http://www.mail-archive.com/[email protected]/msg04986.html

you need to rename enum1.X or enum2.x to some other name so that they don't conflict.

enum Enum1{
    W=0;
    Y=1;    
}
message SomeClass{
    required  Enum1 enum1=1;
    required Enum2 enum2=0;
}
enum Enum2{
    X=0;
    Z=1;    
}

if you really cant because of application dependencies i guess you need to redesign your program somehow..

2 Comments

I use Java but when I compile my .proto file using the protoc compiler , I get the error I mentioned .
But in this way u mentioned , Enum1 will always have the value of enum1.Y .... is there other suggestions !!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.