I want to implement the Either monad in Java.
With the following code, I get the error Erasure of method Either(V) is the same as another method in type Either<T,V>
I don't see how T and V are type erased to the same type so that it causes an error.
public class Either<T, V> {
private T t;
private V v;
public Either(T t) {
this.t = t;
}
public Either(V v) {
this.v = v;
}
}
Related posts, but not duplicates:
The following posts address two constructors, one with Collection<T>
and another with Collection<V>
. The problem is that both parameters to the constructors will be type erased to Collection
and therefore they have the same type signature.
My post is not a duplicate since the primary types are different.
- Erasure of method is the same as another method in type
- Java name clash error, a method has the same erasure as another method
The following post addresses methods, one with compareTo(Real r)
and another with compareTo(Object o)
My post is not a duplicate since the types T
and V
are not related, at least not in a way that I see.
Questions
Why does this type-erasure error occur?
How do I resolve this error to implement the Either monad in Java?