我遵循 Horstman 书中(第 291-292 页)中的示例代码,在 简单的 Scala 角色系统中将案例类定义和使用为消息。
问题是在接收模式匹配中没有识别案例类,并且控制权落入了case _ =>语句。
代码如下所示。一切都适用于非案例类消息。
SENDER:在actor Rcoord 中,act方法是:
def act() {
alive(9000)
register('rcoord, self)
proc_reg.start // start the process register actor
loop {
try {
receive {
case 'process =>
process_counter += 1
process_number = process_counter
spawn_process(process_number, sprocs)
case class CreateTS(xxx: Int)
proc_reg ! CreateTS(process_number)
case 'stats =>
Console.println("received msg from client to view statistics")
//sender ! 'ok
case 'stop =>
Console.println("received msg that client is terminating")
//sender ! 'bye
} // end receive
}
catch
{
RECEIVER:在 actor proc_reg中,act方法是:
def act() {
Console.println("process register started")
case class CreateTS(process_number: Int)
case class UpdateTS(process_number: Int)
loop {
receive {
case CreateTS(process_number) =>
Console.println("create TS msg received from process "+process_number)
register_proc(process_number)
case UpdateTS(process_number) =>
Console.println("update TS msg received for process "+process_number)
update_proc(process_number)
case _ =>
Console.println("sender is: "+sender.toString)
//Console.println("full msg is: "+Msg.toString)
} // end receive
} // end loop
} // end act()