The compile error says that you need to provide an A
mix-in to instantiate a Z
object. As om-non-nom suggests, your code compiles with a small change,
trait A
class Z { this: A =>
}
class Y { this: A =>
val z = new Z with A
}
Here's an alternative that uses inheritance instead of a self-type, and may be closer to your intentions:
trait Y extends A {
val z = new Z with Y
}
EDIT
To answer your updated question, the self-type is a constraint on the type construction. Self-types are different from inheritance in that they don't extend the type's external interface.
From the Scaladoc you linked, it looks like this is the situation:
trait ExtendedDriver extends ExtendedProfile
trait ExtendedProfile { self: ExtendedDriver => }
class Z { this: ExtendedProfile => }
class Y {
this: ExtendedProfile =>
val z = new Z() with ExtendedProfile
}
The problem is that ExtendedProfile
does not inherit from ExtendedDriver
, so it can't stand alone; it needs to be provided an explicit ExtendedDriver
. You can do that with
new Z() with ExtendedProfile with ExtendedDriver
That's actually redundant, since ExtendedProfile
is already mixed in. You simply need,
new Z() with ExtendedDriver