scala - 在 Scala 3 中,为什么有时可以进行谓式类型赋值?

标签 scala scala-3 higher-kinded-types impredicativetypes

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/

相关文章:

scalac : Token not found:/Users/mpa/Library/Caches/JetBrains/IntelliJIdea2021. 3/编译服务器/ token /54149

scala - 在 Scala 3 中,有没有办法禁用 -language :strictEquality (multiversal equality) in a region?

scala - 正确使用术语 Monoid

scala - 在 Scala 3 中是否可以通过泛型类型进行模式匹配?

scala - 高级类型成员声明之间的区别

scala - 带或不带 '_' 的高级类型构造函数

haskell - 通过状态计算分段创建结果,具有良好的人体工程学原理

scala - 转换 "hollow"更高种类的类型值以避免实例化

scala - 如何在 sbt 中命令执行测试?

scala - 如何过滤列表[选项]中的“无”?