路径相关类型是子类型吗? [英] Is a Path Dependent Type a subtype?

查看:97
本文介绍了路径相关类型是子类型吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

trait A {
  trait B {
    def foo: A.this.B = new B{}
    def bar: A#B      = foo 
    def baz: A.this.B = bar // type mismatch; found : A#B required: A.this.B 
  }
}

我是不是说A.this.B是依赖于路径的类型? (到目前为止,这是我的理解) 上面的示例是否表示类型A.this.B A#B的子类型? (如果是,我想区别是A.this.B的实例与A#B的引用相比,对A的实例的引用不是?) 有谁知道能解决我对这两种类型的困惑的启发性解释?

Am I right that A.this.B is a path dependent type?! (That's my understanding so far) Does the example above mean that the type A.this.B is a subtype of A#B? (If yes, I guess the difference is that an instance of A.this.B has a reference to the instance of A compared to A#B which doesn't?) Does anyone know an enlightening explanation that resolves my confusion with these two types?

推荐答案

出色的《 Scala编程》一书非常好

The excellent book Programming in Scala has a pretty good explanation:

class Outer {
  class Inner
}

在Scala中,内部类使用表达式Outer#Inner而不是Java的Outer.Inner进行寻址. .语法是为对象保留的.例如,假设您实例化两个Outer类型的对象,如下所示:

In Scala, the inner class is addressed using the expression Outer#Inner instead of Java's Outer.Inner. The . syntax is reserved for objects. For example, imagine you instantiate two objects of type Outer, like this:

val o1 = new Outer
val o2 = new Outer

此处o1.Innero2.Inner是两种与路径相关的类型(它们是不同的类型).这两种类型都符合更通用的类型Outer#Inner(是其子类型),该类型表示具有一个外部类型为Outer的任意外部对象的Inner类.相比之下,类型o1.Inner引用具有特定外部对象(从o1引用的一个)的内部类.同样,类型o2.Inner引用具有不同的特定外部对象(从o2引用的那个)的内部类.

Here o1.Inner and o2.Inner are two path-dependent types (and they are different types). Both of these types conform to (are subtypes of) the more general type Outer#Inner, which represents the Inner class with an arbitrary outer object of type Outer. By contrast, type o1.Inner refers to the Inner class with a specific outer object (the one referenced from o1). Likewise, type o2.Inner refers to the Inner class with a different, specific outer object (the one referenced from o2).

在Scala中,就像在Java中一样,内部类实例持有对封闭的外部类实例的引用.例如,这允许内部类访问其外部类的成员.因此,如果不以某种方式指定外部类实例,则无法实例化内部类.一种方法是在外部类的主体内部实例化内部类.在这种情况下,将使用当前外部类实例(从中引用).另一种方法是使用依赖于路径的类型.例如,由于类型o1.Inner命名了特定的外部对象,因此您可以实例化它:

In Scala, as in Java, inner class instances hold a reference to an enclosing outer class instance. This allows an inner class, for example, to access members of its outer class. Thus you can't instantiate an inner class without in some way specifying an outer class instance. One way to do this is to instantiate the inner class inside the body of the outer class. In this case, the current outer class instance (referenced from this) will be used. Another way is to use a path-dependent type. For example, because the type, o1.Inner, names a specific outer object, you can instantiate it:

scala> new o1.Inner
res1: o1.Inner = Outer$Inner@13727f

生成的内部对象将包含对其外部对象的引用,该对象是从o1引用的对象.相比之下,由于类型Outer#Inner没有命名Outer的任何特定实例,因此您无法创建它的实例:

The resulting inner object will contain a reference to its outer object, the object referenced from o1. By contrast, because the type Outer#Inner does not name any specific instance of Outer, you can't create an instance of it:

scala> new Outer#Inner
<console>:6: error: Outer is not a legal prefix for
  a constructor
       new Outer#Inner
                 ^

这篇关于路径相关类型是子类型吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆