如果trait以self开头是什么意思:Actor

时间:2016-08-23 12:36:41

标签: scala function traits

我不是一个很好的Scala程序员,需要一些帮助来理解语法。以下是我正在努力的trait

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 _ =>
    }
}

似乎特质的主体以Function1字面开头。 self:Actor => ...在这个例子中,这意味着什么?

1 个答案:

答案 0 :(得分:4)

它定义了Actor类的依赖关系,这意味着任何扩展ActorTracing的类也应该扩展Actor。这基本上建立在Scala中的DI和Cake模式之上,这是构建软件层次结构的想法。

所以在这个例子中,它基本上是说ActorTracing不能用于任何不扩展Actor

的内容

这是一篇非常好的文章。 http://jonasboner.com/real-world-scala-dependency-injection-di/