Can't figure out why is this wrong or how to resolve it. Here's "distilled" code that reproduces the problem. Please help, but I'd appreciate none of the "why" questions - there are very real and valid answers to that but they are proprietary and unchangeable, thus irrelevant to the solution.
object Sandbox {
// --- Following are really Java interfaces/classes ---
trait R[X <: U[X,Y], Y <: E[X,Y]];
class U[X <: U[X,Y], Y <: E[X,Y]] extends R[X,Y];
class E[X <: U[X,Y], Y <: E[X,Y]] extends R[X,Y];
trait R2 extends R[U2,E2];
class U2 extends U[U2,E2] with R2;
class E2 extends E[U2,E2] with R2;
// --- End Java interfaces/classes ---
def trouble[X <: U[X,Y], Y <: E[X,Y], Z <: R[X,Y]](r: Z) {}
def main(args: Array[String]) {
trouble(new U()); // Fine
trouble(new E()); // Fine
trouble(new U2()); // Not fine, reports:
/*
* inferred type arguments [Nothing,Nothing,Sandbox.U2]
* do not conform to method trouble's type parameter bounds
* [X <: Sandbox.U[X,Y],Y <: Sandbox.E[X,Y],Z <: Sandbox.R[X,Y]]
*/
trouble(new E2()); // Not fine, reports:
/*
* inferred type arguments [Nothing,Nothing,Sandbox.E2]
* do not conform to method trouble's type parameter bounds
* [X <: Sandbox.U[X,Y],Y <: Sandbox.E[X,Y],Z <: Sandbox.R[X,Y]]
*/
trouble[U2,E2,R2](new U2()); // Fine
trouble[U2,E2,R2](new E2()); // Fine
}
}
The compiler can't seem to infer X, Y and Z type args of the "trouble" method just based on the single argument specified. I understand that much - when I specify the types, it is OK, but it is very cumbersome. Is there a way to nudge/help the compiler in some way such that this stops being a problem?
Maybe I am placing too much confidence in Scala's type inference system, but all the information is available to it.
Thanks in advance!