tim
tim

Reputation: 121

Bug in Scala's type system?

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]] )

Upvotes: 5

Views: 1229

Answers (5)

lisak
lisak

Reputation: 21961

It's all explained here, focus on the "Common Pitfalls" section because it is quite TLTR.

Upvotes: 0

Anonymous
Anonymous

Reputation: 841

You don't wan't your class to take ONE type parameter, you wan't it 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 this:

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

Upvotes: 0

Craig P. Motlin
Craig P. Motlin

Reputation: 26740

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.

Upvotes: 6

Knut Arne Vedaa
Knut Arne Vedaa

Reputation: 15742

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.)

Upvotes: 15

Madoc
Madoc

Reputation: 5919

You didn't declare X as a type parameter for C. Try the following:

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

Upvotes: 2

Related Questions