[英]Why do proper types Any and Nothing fit type constructor shape F[_] when they are different kinds?
考慮以下采用* -> *
類型參數的方法
def g[F[_]] = ???
為什么以下不是語法錯誤
g[Any] // ok
g[Nothing] // ok
自從
scala> :kind -v Any
Any's kind is A
*
This is a proper type.
scala> :kind -v Nothing
Nothing's kind is A
*
This is a proper type.
所以Any
和Nothing
應該是錯誤的形狀?
引自 Scala 規格:
對於每個類型構造函數
(帶有任意數量的類型參數),
scala.Nothing <: <: scala.Any
。
https://scala-lang.org/files/archive/spec/2.13/03-types.html#conformance
假設類型參數的下限為
1,…,
上限1,…,
。 如果每個實際類型參數都符合其邊界,則參數化類型是格式良好的,即<:<:
where是替換
[1:=1,…,:=]
。
https://scala-lang.org/files/archive/spec/2.13/03-types.html#parameterized-types
多態方法類型在內部表示為
[tps]
其中[tps]
是類型參數部分[1 >: 1 <: 1,…, >: <: ]
對於某些≥0
和是(值或方法)類型。 此類型表示采用 arguments
1,…,
它符合下限1,…,
和上限1,…,
並產生類型的結果.
https://scala-lang.org/files/archive/spec/2.13/03-types.html#polymorphic-method-types
因此,由於Any
和Nothing
符合F[_]
的上限和下限(即Any
和Nothing
對應), g[Any]
和g[Nothing]
是合法的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.