Pie drawing: mixing in tag

I play with a pie template and I don’t understand something there.

Given the following common code:

trait AServiceComponent {
  this: ARepositoryComponent =>
}

trait ARepositoryComponent {}

the next way to mix them works

trait Controller {
  this: AServiceComponent =>
}

object Controller extends 
  Controller with 
  AServiceComponent with 
  ARepositoryComponent

But the following:

trait Controller extends AServiceComponent {}

object Controller extends
  Controller with
  ARepositoryComponent

with an error:

illegal inheritance; self-type Controller does not conform to AServiceComponent selftype AServiceComponent with ARepositoryComponent

Can't we "click" dependencies in a hierarchy if we know that they will be distributed to all subclasses?

Should the compiler be allowed to Controllerhave dependencies if it was not created without their permission?

+4
source share
1 answer

Here is a slightly simpler way to run the same problem:

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 selftype Bar with Foo
       trait Baz extends Bar
                         ^

The problem is that the compiler expects you to repeat the self constraint in the subtype definition type. In my simplified case, we will write:

trait Baz extends Bar { this: Foo => }

:

trait Controller extends AServiceComponent { this: ARepositoryComponent => }

: , -, Controller, , , .

+3

Source: https://habr.com/ru/post/1533536/


All Articles