Error in a system like Scala?

The following scala code appears to be valid:

class A[X]
class C[M[X] <: A[X]]

class Main

new C[A]

I expected the compiler to execute type inference in type A, but after I tried the following:

new C[A[Int]]

The following error message appeared:

(fragment of Main.scala):11: error: this.A[Int] takes no type parameters, expected: one
println( new C[A[Int]] )
+5
source share
5 answers

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 parameter of the type, which should be a class that takes one parameter of type AND, parameterized, is a subclass of class A, parameterized with the same type.

When you write

new C[A]

: C A. ? , , .

,

new C[A[Int]]

, C, A [Int], : A [Int] , . ( A [X].)

+15

.

class C[M <: A[_]]

, C - , , A .

+6

X C. :

class C[X, M[X] <: A[X]]
+2

, " ", TLTR.

+1
source

You do not want your class to accept ONE type parameter, you do not need to take two! Two possible solutions:

class A[X] {
     type T = X
}
class C[M <: A[_]] {
     //use M#T if you want the type T was parameterized with.
}

Or you can do:

class A[X]
class C[T, M[A] <: A[A]] {
     //when you want the type, write M[T], not M.
}

HOWEVER, what you probably want is:

class A[X]
class C[M <: A[_]]
0
source

All Articles