2

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?

user3663882
  • 6,957
  • 10
  • 51
  • 92
  • May be duplicated: http://stackoverflow.com/questions/10291176/how-to-use-scala-trait-with-self-reference – pianista Aug 23 '16 at 12:45

1 Answers1

4

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/

Stephen Carman
  • 999
  • 7
  • 25
  • 1
    As note, "any trait that extends ActorTracing should also extend Actor" it's not correct, any `class` that extends that trait has to also extend `Actor`, `trait` don't necessarily have to. – Ende Neu Aug 23 '16 at 14:14