The inner type Q
is defined only for specific instance implementation of the T
trait. Since scala has path-dependent types, each instance of T
will have his own subtrait Q
.
scala> trait T {
| trait Q
| }
defined trait T
scala> class C extends T {
| def getQ: this.Q = new this.Q {}
| }
defined class C
scala> val inC = (new C).getQ
inC: C#Q = C$$anon$1@3f53073a
scala> val c = new C
c: C = C@1a7e4ff0
scala> new c.Q {}
res4: c.Q = $anon$1@36bbb2f5
If you need an interface for a generic behavior for your clients to implement, and not dependent on a specific C
instance, you should define it within an Object
scala> object T {
| trait Q {
| def implementMe: Unit
| }
| }
defined module T
scala> val inT = new T.Q {
| def implementMe = println("implemented!")
| }
inT: T.Q = $anon$1@20f2a08b
scala> inT.implementMe
implemented!
Why path-dependent types?
As for the design reasons, look here
T.Q
isT#Q
. – OmentumT#Q
syntax Oleg referred to is called a type projection. And note that you can extend Q if you do it within a type derived from T, e.g.class A extends T { class B extends Q }
. – Gentlewoman