繁体   English   中英

为什么正确的类型 Any 和 Nothing 适合类型构造函数形状 F[_] 当它们是不同的类型时?

[英]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.

所以AnyNothing应该是错误的形状?

引自 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

因此,由于AnyNothing符合F[_]的上限和下限(即AnyNothing对应), g[Any]g[Nothing]是合法的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM