Error, higher Scala types: type arguments do not match. type T is limited than declared boundaries of type T

Here is a simple experiment in the Scala REPL:

scala> trait A; trait B extends A; trait C extends B defined trait A defined trait B defined trait C scala> trait TC[T] defined trait TC scala> trait TC2[T <: B] defined trait TC2 scala> class Test[TC[T]] warning: there was one feature warning; re-run with -feature for details defined class Test scala> new Test[TC] res1: Test[TC] = Test@6f195bc3 scala> new Test[TC2] <console>:11: error: kinds of the type arguments (TC2) do not conform to the expected kinds of the type parameters (type TC) in class Test. TC2 type parameters do not match type TC expected parameters: type T (in trait TC2) bounds <: B are stricter than type T declared bounds >: Nothing <: Any val res2 = ^ <console>:12: error: kinds of the type arguments (TC2) do not conform to the expected kinds of the type parameters (type TC) in class Test. TC2 type parameters do not match type TC expected parameters: type T (in trait TC2) bounds <: B are stricter than type T declared bounds >: Nothing <: Any new Test[TC2] ^ 

Question 1:

How to explain these Scala Language Specification error messages?

In other words, which sections of the SLS explain these error messages?

Question 2: how can these error messages be explained in simple terms (not based on SLS)?

Phrasing the previous question in the words of the compiler:

why is it a problem that TC2 type parameters do not match type TC expected parameters , i.e. type T (in trait TC2) bounds <: B are stricter than type T declared bounds >: Nothing ?

Is there any book or article explaining the reasons for this error message?

Perhaps somewhere in Pierce TAPL's book?

+5
source share
2 answers

As I noted in a previous comment, the TC in the list of parameters of type Test (and in the error message) is not the TC that you defined a few lines earlier - this is a new constructor type that obscures the TC flag.

(As a side note, I would strongly recommend not using shadow type parameters. Shadow variables at the value level can be misleading, but shading type parameters are almost always a recipe for confusion.)

Type constructor parameters are discussed in section 4.4 of the specification. From this section:

A type constructor parameter adds a sentence of a nested type parameter to a type parameter ... The above coverage restrictions are generalized to the case of nested type parameter sentences that declare higher-order parameters. Type parameters of a higher order (type parameters of a parameter of type t) are visible only in their immediately surrounding parameter sentence (possibly including sentences at a deeper level of nesting) and within t.

Your T here is one of these higher order parameters. It can be limited (like any other type parameter), but it is not. This causes an error: you are trying to provide a type constructor that constrains its type parameter ( TC2 ) as the value of the parameter of the type constructor that does not share the constraint (in fact, it does not have a constraint at all).

To get an intuitive idea of ​​why this is a problem, consider the following characteristic:

 trait Foo[X[_]] { def create[A]: X[A] } 

This is a perfectly reasonable thing to write: I could create an instance, for example, as follows:

 object ListFoo extends Foo[List] { def create[A]: List[A] = Nil } 

Now suppose I have a type constructor with a constraint:

 trait MyIntOptionThingy[A <: Option[Int]] 

The compiler forbids me to create an instance of Foo[MyIntOptionThingy] , because a parameter of type MyIntOptionThingy more strictly limited to a parameter of the type of a list of parameters of type X in Foo . If you think about it, it makes sense: how could I define create for any A when the only A that would work on MyIntOptionThingy are Some[Int] , None.type and Option[Int] ?

+7
source

For completeness, I quote some, possibly relevant parts of SLS:

enter image description here

About the conformity of type constructors from SLS 3.5.2:

enter image description here

0
source

Source: https://habr.com/ru/post/1210479/


All Articles