Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bug in Scala's type system?

Tags:

scala

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]] )
like image 644
tim Avatar asked Jan 06 '11 11:01

tim


2 Answers

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

like image 149
Knut Arne Vedaa Avatar answered Oct 05 '22 22:10

Knut Arne Vedaa


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.

like image 33
Craig P. Motlin Avatar answered Oct 05 '22 23:10

Craig P. Motlin