Dưới đây là một thử nghiệm đơn giản trong Scala REPL:Lỗi, Loại được đánh giá cao hơn Scala: đối số kiểu không phù hợp. gõ giới hạn T là nghiêm ngặt hơn loại T tuyên bố giới hạn
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] = [email protected]
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's type parameters do not match type TC's expected parameters:
type T (in trait TC2)'s bounds <: B are stricter than type T's 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's type parameters do not match type TC's expected parameters:
type T (in trait TC2)'s bounds <: B are stricter than type T's declared bounds >: Nothing <: Any
new Test[TC2]
^
Câu hỏi 1:
Làm thế nào có thể các thông báo lỗi được giải thích dựa trên Scala Language Specification ?
Nói cách khác, các phần nào của SLS giải thích các thông báo lỗi này?
Câu hỏi 2:, làm thế nào có thể các thông báo lỗi được giải thích trong thuật ngữ đơn giản (không dựa trên SLS)?
phân nhịp câu hỏi trước trong lời nói của trình biên dịch:
lý do tại sao nó là một vấn đề mà TC2's type parameters do not match type TC's expected parameters
, ví dụ: type T (in trait TC2)'s bounds <: B are stricter than type T's declared bounds >: Nothing
?
Có bất kỳ cuốn sách hoặc bài viết nào mà lý do đằng sau thông báo lỗi này được giải thích không?
Có lẽ đâu đó trong cuốn sách TAPL của Pierce?
Lưu ý rằng bạn' re shadowing 'TC' trong' Test'. Sử dụng tên biến mới (ví dụ: 'kiểm tra lớp [X [T]]') làm cho thông báo rõ ràng hơn. –