scala - 路径相关类型是类型投影吗?

标签 scala types projection path-dependent-type

我现在正在深入阅读 Scala。以下是本书的摘录:

All path-dependent types are type projections. A path-dependent type foo.Bar is rewritten as foo.type#Bar by the compiler...

In Scala, all type references can be written as projects against named entities. The type scala.String is shorthand for scala.type#String where the name scala refers to the package scala and the type String is defined by the String class on the scala package.

显然,没有 scala.String 类,但我无法使用 Null 重现它。

scala> type N = scala.type#Null
<console>:7: error: type mismatch;
 found   : type
 required: AnyRef
       type N = scala.type#Null

所以,我的问题如下。路径相关类型是类型投影吗?它只是内部编译器表示还是可以用 scala 代码表示?

最佳答案

这是一个快速的 REPL session ,它证实了 Josh 所写的内容,

scala> class Foo { type T = String }
defined class Foo

scala> val foo = new Foo
foo: Foo = Foo@10babe8

scala> implicitly[foo.type#T =:= foo.T]
res0: =:=[foo.T,foo.T] = <function1>

您的 scala.type#Null 示例的问题在于前缀 scala 是包前缀,而不是值的稳定标识符。按理说它应该是后者,但不幸的是它不是......这是 Scala 包和 Scala 对象(在模块意义上)的语义之间挥之不去的不匹配。

关于scala - 路径相关类型是类型投影吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10554686/

相关文章:

.Net 使用反射来定义 OfType

python - Pandas Dataframe 将列解释为 float 而不是 String

python - 将 C 数据类型转换为 Python

java - struts2中需要的简单OGNL投影

Three.js - 从另一个网格的某些面/顶点创建新网格

scala - 如何在 Scala 中进行默认方法处理

scala - 如何将非托管依赖项导入到代码中

scala - 在DataFrameWriter上使用partitionBy会使用列名而不只是值来写入目录布局

java - 获取配置的路由的基本路径

linq - 将 LINQ/AutoMapper 项目展平至嵌套子项