Reputation: 2186
I have a complex use case of generics type which has been simplified below
trait A
class AB extends A{
val v = 10
}
trait X[T<:A]{
def request: T
}
class XY extends X[AB]{
def request = new AB()
}
class Test extends App{
/**
* X[A]
* X[AB]
* XY[A]
* XY[AB]
*/
def test[C<:A, D <: X[C]](t:Int)(input: D): Unit ={
print(input.getClass.getName)
}
implicit val req = new XY()
test(2)(req)
}
test method should support the Type scenarios defined in comment section. I'm getting the below compilation error.
Error:(33, 7) inferred type arguments [XY] do not conform to method test's type parameter bounds [D <: X[Nothing]] test(2)(req)
Is this syntactically legal? Thanks in advance.
Upvotes: 2
Views: 204
Reputation: 14224
The compiler can't infer the type of C
in 2 steps with a definition like this.
So either have the compiler do it in 1 step, by having both D
and C
in the definition of the input
argument:
def test[C <: A, D <: X[C]](t: Int)(input: D with X[C]): Unit
Or have an implicit evidence for D <: X[C]
, that will help the compiler to infer C
in 2 steps:
def test[C <: A, D <: X[_]](t: Int)(input: D)(implicit ev: D <:< X[C]): Unit
Upvotes: 3
Reputation: 51658
Nothing
in compile error usually means that some type wasn't inferred.
Try to specify type parameters explicitly
test[AB, XY](2)(req)
Generic nested type inference works with arity-2 but not with currying
Upvotes: 2