Scala 3 是一种命令式语言,通常不可能将更高类型分配给普通类型(存在触发吉拉德悖论的风险),但实际上,某些类型分配似乎能够绕过此规则:
trait Vec[T]
type VV1 = Vec // paradox!
type VV2 = [T] =>> Vec[T] // no paradox?
最后两行有什么区别?为什么第二种可能?
最佳答案
第二行是可能的,因为语言作者决定类型别名可以包含类型构造函数以保持一致性。如果 List
是一种类型,尽管不是正确的类型,那么如果我们想保持一致,那么可以是类型别名。如果函数可以柯里化(Currying)、部分应用或传递,那么类型也可以。
trait Vec[T]
type VV1 = Vec // not! a paradox, but shorthand for type VV1 = [T] =>> Vec[T]
第二行等于第三行:
A parameterized type definition
type T[X] = R
is regarded as a shorthand for an unparameterized definition with a type lambda as right-hand side:
type T = [X] =>> R
A partially applied type constructor such as
List
is assumed to be equivalent to its eta expansion. I.e,List = [X] =>> List[X]
. This allows type constructors to be compared with type lambdas.
参见specification了解更多详情。
关于scala - 在 Scala 3 中,为什么有时可以进行谓式类型赋值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76258464/