我不是一个很好的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 =>
...在这个例子中,这意味着什么?
答案 0 :(得分:4)
它定义了Actor类的依赖关系,这意味着任何扩展ActorTracing
的类也应该扩展Actor
。这基本上建立在Scala中的DI和Cake模式之上,这是构建软件层次结构的想法。
所以在这个例子中,它基本上是说ActorTracing
不能用于任何不扩展Actor
这是一篇非常好的文章。 http://jonasboner.com/real-world-scala-dependency-injection-di/