Is a Path Dependent Type a subtype?

前端 未结 3 807
花落未央
花落未央 2021-01-06 07:18
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 
  }
         


        
相关标签:
3条回答
  • 2021-01-06 07:50

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

    class Outer {
      class Inner
    }
    

    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
    

    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).

    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
    

    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
                     ^
    
    0 讨论(0)
  • 2021-01-06 08:07

    In short: Yes it is

    If you want, you can consider A#B to have an abstract reference to the containing A (and therefore not directly constructable, as for any abstract type), this reference being made concrete in the path-dependent subclasses.

    0 讨论(0)
  • 2021-01-06 08:10

    Yes, it's a path dependent type. Yes, all instances of B are members of A#B. Note that you can't directly construct an instance of A#B, all instances of B must have a reference to the outer A instance

    0 讨论(0)
提交回复
热议问题