我现在正在深入阅读 Scala。以下是本书的摘录:
All path-dependent types are type projections. A path-dependent type
foo.Bar
is rewritten asfoo.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 namescala
refers to the packagescala
and the typeString
is defined by theString
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/