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
}
}
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?
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
^
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With