user3663882 user3663882 - 2 months ago 7x
Scala Question

What does it mean if trait starts with self: Actor

I don't quite a good Scala programmer and need some help with understanding syntax. Here is the trait I am struggling with:

trait ActorTracing extends AroundReceiveOverrideHack { self: Actor =>

protected def serviceName: String =

implicit def any2response[T](msg: T): ResponseTracingSupport[T] =
new ResponseTracingSupport(msg)

implicit lazy val trace: TracingExtensionImpl =

override protected final def aroundReceiveInt(receive: Receive, msg: Any): Unit =
msg match {
case ts: BaseTracingSupport if receive.isDefinedAt(msg) =>
trace.start(ts, serviceName)
case _ =>

It seems that the traits' body starts with
self:Actor =>
... What would it mean here in this example?


It defines a dependency of the Actor class meaning that any class that extends ActorTracing should also extend Actor. This basically builds on the DI and the Cake pattern in Scala which is the idea of building layers up of software.

So in this example it is basically saying that ActorTracing cannot be used on anything that doesn't also extend Actor

Here is a really good article about this.