简体   繁体   English

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

[英]Why do proper types Any and Nothing fit type constructor shape F[_] when they are different kinds?

Consider the following method which takes type parameter of * -> * kind考虑以下采用* -> *类型参数的方法

def g[F[_]] = ???

Why is the following not a syntax error为什么以下不是语法错误

g[Any]       // ok
g[Nothing]   // ok

since自从

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.

so Any and Nothing should be of wrong shape?所以AnyNothing应该是错误的形状?

Quotes from Scala spec:引自 Scala 规格:

For every type constructor对于每个类型构造函数(with any number of type parameters), scala.Nothing <: <: scala.Any . (带有任意数量的类型参数), scala.Nothing <: <: scala.Any

https://scala-lang.org/files/archive/spec/2.13/03-types.html#conformance https://scala-lang.org/files/archive/spec/2.13/03-types.html#conformance

Say the type parameters have lower bounds 1,…, and upper bounds 1,…, .假设类型参数的下限为1,…,上限1,…, The parameterized type is well-formed if each actual type parameter conforms to its bounds, ie <:<: where如果每个实际类型参数都符合其边界,则参数化类型是格式良好的,即<:<: where is the substitution [1:=1,…,:=] .是替换[1:=1,…,:=]

https://scala-lang.org/files/archive/spec/2.13/03-types.html#parameterized-types https://scala-lang.org/files/archive/spec/2.13/03-types.html#parameterized-types

A polymorphic method type is denoted internally as [tps] where [tps] is a type parameter section [1 >: 1 <: 1,…, >: <: ] for some ≥0 and多态方法类型在内部表示为[tps]其中[tps]是类型参数部分[1 >: 1 <: 1,…, >: <: ]对于某些≥0is a (value or method) type.是(值或方法)类型。 This type represents named methods that take type arguments 1,…, which conform to the lower bounds 1,…, and the upper bounds 1,…, and that yield results of type此类型表示采用 arguments 1,…,它符合下限1,…,和上限1,…,并产生类型的结果. .

https://scala-lang.org/files/archive/spec/2.13/03-types.html#polymorphic-method-types https://scala-lang.org/files/archive/spec/2.13/03-types.html#polymorphic-method-types

So since Any and Nothing conform to upper and lower bounds of F[_] (namely, Any and Nothing correspondingly), g[Any] and g[Nothing] are legal.因此,由于AnyNothing符合F[_]的上限和下限(即AnyNothing对应), g[Any]g[Nothing]是合法的。

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

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