The following scala code seems to be valid:
class A[X]
class C[M[X] <: A[X]]
class Main
new C[A]
I expected the compiler to perform type inference on type A, but after I tried the following:
new C[A[Int]]
I got the following error message:
(fragment of Main.scala):11: error: this.A[Int] takes no type parameters, expected: one
println( new C[A[Int]] )
Let's see what this means in plain English.
class A[X]
means: let A be a class that takes one type parameter.
class C[M[X] <: A[X]]
means: let C be a class that takes one type parameter, which should be a class that takes one type parameter AND, parameterized, is a subclass of class A parameterized with the same type.
When you write
new C[A]
you're saying: create an instance of C with A as parameter. Does A conform to the criteria above? Yes, it's a class that takes one type parameter, and parameterized it is a subclass of itself parameterized.
However, when you write
new C[A[Int]]
the type parameter you're trying to give C, A[Int], does not conform to the criteria: A[Int] does not take any type parameters, which the compiler kindly tells you. (And it is not a subclass of A[X] either.)
Try this syntax.
class C[M <: A[_]]
This means that C is a class that takes one type parameter, which should be a subclass of A and takes one type parameter.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With