Why do generic types have the same signature?
generics, java
Solution
It seems to me that the two constructors share the same signature although they have different generic type arguments.
They do. The signature is
Or(Object o);
Why?
Because of type erasure implementation of generics in Java: references to generic types are converted to `System.Object` in all contexts where they are used; the generic type is known only to the compiler.
And how to work around this problem?
Unfortunately, you cannot easily work around this problem in a constructor. You can replace the overloaded constructors with factory methods, and give different names, say `OrWithA` and `OrWithB`:
// Hide the constructor
private Or(...) {
...
}
// Publish factory methods
public static <X> Or OrWithA(X a) {
return new Or(...);
}
public static <X> Or OrWithB(X a) {
return new Or(...);
}
Problem
I have the following generic class: ``` class Or<A,B> { Or (A a) {} Or (B b) {} } ``` Why do I get the following error when I try to compile it: ``` Or(A) is already defined in Or Or (B b) ^ ``` It seems to me that the two constructors share the same signature although they have different generic type arguments. Why? And how to work around this problem? Update I understood the problem now. The compiler needs a way to distinguish the two types. Adding such a constrain would be ok for my use case. So I would like to add another question: How to specify that the two types A and B may be anything but different?