I've been playing with the cake pattern and there's something I don't fully understand.
Given the following common code:
trait AServiceComponent {
this: ARepositoryComponent =>
}
trait ARepositoryComponent {}
the following way of mixing them works
trait Controller {
this: AServiceComponent =>
}
object Controller extends
Controller with
AServiceComponent with
ARepositoryComponent
But the following does not
trait Controller extends AServiceComponent {}
object Controller extends
Controller with
ARepositoryComponent
with error:
illegal inheritance; self-type Controller does not conform to AServiceComponent's selftype AServiceComponent with ARepositoryComponent
Shouldn't we be able to "push" dependencies up in the hierarchy if we know that they will be common for all subclasses?
Shouldn't the compiler allow for Controller
to have dependencies, as long as it's not instantiated without resolving them?
Here's a slightly simpler way to run into the same issue:
scala> trait Foo
defined trait Foo
scala> trait Bar { this: Foo => }
defined trait Bar
scala> trait Baz extends Bar
<console>:9: error: illegal inheritance;
self-type Baz does not conform to Bar's selftype Bar with Foo
trait Baz extends Bar
^
The problem is that the compiler expects you to repeat the self-type constraint in the subtype definition. In my simplified case we'd write:
trait Baz extends Bar { this: Foo => }
In yours you just need to make the following change:
trait Controller extends AServiceComponent { this: ARepositoryComponent => }
This requirement makes some sense—it's reasonable to want someone using Controller
to be able to know about this dependency without looking at the types it inherits from.
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