[英]Are path-dependent types type projections?
I'm reading Scala in depth now. 我现在正在深入阅读Scala。 Here is an excerpt from the book: 以下是本书的摘录:
All path-dependent types are type projections. 所有与路径相关的类型都是类型投影。 A path-dependent type
foo.Bar
is rewritten asfoo.type#Bar
by the compiler... 路径依赖类型foo.Bar
由编译器重写为foo.type#Bar
...In Scala, all type references can be written as projects against named entities. 在Scala中,所有类型引用都可以编写为针对命名实体的项目。 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类型是scala.type#String
缩写,其中名称scala
引用包scala
,类型String
由scala包上的String
类定义。
Obviously, there isn't scala.String
class, but I failed to reproduce this with Null
. 显然,没有scala.String
类,但我无法用Null
重现这个。
scala> type N = scala.type#Null
<console>:7: error: type mismatch;
found : type
required: AnyRef
type N = scala.type#Null
So, my questions are as follows. 所以,我的问题如下。 Are path-dependent types type projections? 路径依赖类型是否类型预测? Is it just inner compiler representation or can be expressed in scala code? 它只是内部编译器表示还是可以用scala代码表示?
Here's a quick REPL session which confirms what Josh wrote, 这是一个快速的REPL会话,它确认了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>
The problem with your scala.type#Null
example is that the prefix scala
is a package prefix rather than being a stable identifier of a value. scala.type#Null
示例的问题是前缀scala
是包前缀,而不是值的稳定标识符。 Arguably it ought to be the latter, but unfortunately it's not ... that's a lingering mismatch between the semantics of Scala packages and Scala objects (in the sense of modules). 可以说它应该是后者,但不幸的是它不是......这是Scala包和Scala对象(在模块意义上)的语义之间挥之不去的不匹配。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.