Difference between trait inheritance and self type annotation

Ben Lings picture Ben Lings · Feb 8, 2010 · Viewed 8.1k times · Source

In Scala, I've seen the constructs

trait T extends S

and

trait T { this: S =>

used to achieve similar things (namely that the abstract methods in S must be defined before an instance may be created). What's the difference between them? Why would you use one over the other?

Answer

Joa Ebert picture Joa Ebert · Feb 8, 2010

Self type annotations allow you to express cyclic dependencies. For instance:

trait A extends B
trait B { self: A => }

This is not possible with simple inheritance.