user3663882 user3663882 - 3 months ago 11
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 =
this.getClass.getSimpleName

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

implicit lazy val trace: TracingExtensionImpl =
TracingExtension(context.system)

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
Function1
literal.
self:Actor =>
... What would it mean here in this example?

Answer

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. http://jonasboner.com/real-world-scala-dependency-injection-di/