Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Self-type does not conform to base class

Using the following code:

trait Hello[B <: Baz[_, _]]
trait World[F <: Foo] { self: Hello[Baz[F, _]] =>

  def foo: F

}
trait Baz[F <: Foo, B <: Bar]
trait Foo
trait Bar

case class BasicHello() extends Hello[BasicBaz] with World[BasicFoo]
case class BasicBaz() extends Baz[BasicFoo, BasicBar]
case class BasicFoo() extends Foo
case class BasicBar() extends Bar

I get the following error in my IDE at the BasicHello case class:

Illegal inheritance, self-type BasicHello does not conform to Hello[Baz[F, _]]

I have no clue why the compiler doesn't allow this since BasicHello extends Hello[BasicBaz], which in its turn extends Baz[BasicFoo, BasicBar]. The type of BasicHello should be Hello[Baz[Foo, Bar]] which is exactly what the World mix-in requires. Is there some hierarchical property I'm missing of self-types?

like image 251
Martijn Avatar asked Mar 08 '23 16:03

Martijn


1 Answers

The problem is that Hello is invariant in B. So, Hello[BasicBaz] is not a subclass of Hello[Baz[BasicFoo,BasicBar]] even though BasicBaz is a subclass of Baz.

If you make Hello covariant in B, (trait Hello[+B <: Baz[_,_]]), then it'll compile.

like image 169
Dima Avatar answered Mar 11 '23 04:03

Dima