monica
monica

Reputation: 1065

Illegal inheritance while extending trait with self type

I'm confused by the following code:

    trait T1 {
        type S
    }

    trait T2 {
      self: T1 =>

       case  class ClS(s: S)
    }

    object O extends T2 {

    }

It gives out the following error message:

illegal inheritance; tests.O.type does not conform to tests.T2's selftype tests.T2 with tests.T1

What's going wrong? the reason that I extend O with T2 is that I want to reference the class CLS in O, how can I achieve that?

Upvotes: 7

Views: 9068

Answers (2)

drexin
drexin

Reputation: 24403

T2 has a self type T1, which means, that every class/object/trait inheriting from T2 has to be of type T1, so in your case

object O extens T1 with T2 {
  type S = Whatever
}

should work.

Upvotes: 6

EECOLOR
EECOLOR

Reputation: 11244

trait T2 has a contract that says: If you inherit from me, you should also inherit from T1.

object O extends T2 with T1

You would then probably get an error that type S is not defined. Which is a good thing since it's used in the definition of CLS

Upvotes: 16

Related Questions