我想将回调传递给其构造函数中的akka actor:
object FSMActorWithCallback {
type Tracer = (Int, NodeState, NodeData, ActorRef, Any) => Unit
}
class FSMActorWithCallback(tracerCallback: FSMActorWithCallback.Tracer) extends FSMAwesomeActor
// method is called each FSM Event so we can record current state and next message
override def trace(state: NodeState, data: NodeData, sender: ActorRef, msg: Any) : Unit = {
// different tracing callback for different test rigs such as unit tests or integration tests
tracerCallback(nodeUniqueId, state, data, sender, msg)
}
}
这将允许我使用定义原始actor,new
但我需要使用actorOf
factory方法来使actor正确挂接到系统中:
class Supervisor extends Actor {
def outputStateTrace(state: NodeState, data: NodeData, sender: ActorRef, msg: Any): Unit = {
/*actually make a binary log for analysis of complex failures*/
}
// COMPILE ERROR "follow this method with _ if you wan to treat it as a partially applied function"
var child = contact.actorOf(Props(classOf[FSMActorWithCallback], outputStateTracer)
// seems to work fine but not what i need
val childRaw = new FSMActorWithCallback(tracer)
}
actor的实际构造需要通过所示的factory方法进行,但我无法弄清楚如何通过factory方法传递回调。
您传递的是方法而不是函数。编译器告诉您使用Eta扩展将其转换为函数。
var child = contact.actorOf(Props(classOf[FSMActorWithCallback], outputStateTracer _)
由于在底部解释这个帖子:
由于我们已经引入了神奇的下划线作为万事通,同时在函数定义中发现了一些快捷方式,因此,如果我们想更明确地启动eta扩展,他将再次加入。因此,迫使编译器将第二种方法强制转换为函数的第二种选择是显式地引用所谓的方法值,方法是简单地在方法名称后加一些下划线(而不是方法参数)。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句