Motivation
I have an Either<L, R>
class, which represents a value of one of two types, or semantically different states. In some cases, it is valuable to operate on it no matter which alternative the value is.
Problem
I want a (non-static) method that takes a Consumer<T>
, where T
is a supertype of both L
and R
, where L
and R
are type parameters for the class.
Currently, java lets me do this: (static implementation)
public static <T, L extends T, R extends T> void collapse(Either<L,R> e, Consumer<T> op)
But of course, with a non-static implementation, I can't impose constraints on L
and R
, because they're already defined for the instance in question. I need those constraints imposed on T
instead, but java won't let me write the following because it only allows one class in a supertype or subtype constraint at once. This is especially frustrating given all classes share at least Object
as a common supertype, so these constraints are always satisfiable.
public void collapse(Consumer<? super L & R> op)
Is there any other way to define this constraint, any hint of it being allowed in a later version of java, or any explanation of why it would be a breaking feature?
? extends Consumer<? super T>
as the consumer type. – Arrester