简体   繁体   English

路径相关类型和类型投影

[英]Path-dependent Types and Type Projection

As I understand the def bar(x: X#Inner) signature, it simply requires any instance of Inner to compile successfully. 据我了解def bar(x: X#Inner)签名,它只需要Inner 任何实例即可成功编译。 Whereas, foo(x: this.Inner) will only compile with the same Outer.Inner class instance. foo(x: this.Inner)将仅使用相同的Outer.Inner类实例进行编译。

While reading Scala in Depth , I put this code into REPL, but I'm getting a compile-time error. 在深入阅读Scala的同时 ,我将此代码放入REPL中,但是遇到编译时错误。

scala> class Outer {
     |    trait Inner
     |     def y = new Inner {}
     |    def foo(x: this.Inner) = null
     |    def bar(x: X#Inner) = null
     | }
<console>:11: error: not found: type X
          def bar(x: X#Inner) = null
                     ^

Also, what is the compile-time error here? 另外,这里的编译时错误是什么?

The X in X#Inner refers to the outer type. XX#Inner指外类型。 You do not have a type X anywhere, that's what the compiler error says exactly. 您在任何地方都没有类型X ,这就是编译器错误确切说明的。

In your example you will want to refer to type Outer instead: 在您的示例中,您将改为引用“ Outer类型:

class Outer {
  trait Inner
  def foo(x: this.Inner) = ???
  def bar(x: Outer#Inner) = ???
}

X#Inner应该是Outer#Inner ,我认为Scala in depth的作者在书中打错了字眼。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM