我是Scala的新手。
在其中一个Actor类中,我看到我的同事定义了这样的代码
导入akka.actor.Actor
class Processor extends Actor {
def receive: Receive = {
case msg: String => doProcess(msg)
case _ =>
}
}
此类接收定义在哪里?它不会导入此类。系统将如何找到Receive类
答案 0 :(得分:6)
它是分别在PartialFunction[Any, Unit]
伴侣对象和特征中定义的akka.actor.Actor
的类型别名。
摘自source code:
object Actor {
// Type alias representing a Receive-expression for Akka Actors.
type Receive = PartialFunction[Any, Unit]
// ...
}
trait Actor {
// to make type Receive known in subclasses without import
type Receive = Actor.Receive
// ...
}
答案 1 :(得分:1)
Receive
是PartialFunction[Any,Unit]
的类型别名。此类型别名在Actor
伴随对象上定义,然后在Actor
特征上重新定义(因此在Actor
impl中可用),引用伴侣上定义的特征。
答案 2 :(得分:1)
Receive是PartialFunction [Any,Unit]
的类型别名查看Actor源代码here
您也可以将接收方法编写为
def receive: PartialFunction[Any, Unit] = {
….
}