3

我尝试编写示例代码来结合 akka 和 actor。但是我在编译代码时收到了错误消息。
代码非常简单,如下所示。
那么,我做错了什么?

[error] /home/qos/workspaces/actors/actors.scala:20: type mismatch;
[error]  found   : Unit
[error]  required: scala.sys.process.ProcessLogger
[error]         execute(cmd)
[error]                ^
[error] one error found
[error] (compile:compile) Compilation failed

代码是

import scala.sys.process._ 
import akka.actor._ 

object TryActor { 

  def main(args: Array[String]) { 
    val akkaSystem = ActorSystem("akkaSystem") 
    val worker = akkaSystem.actorOf(Props[Worker], name = "work0") 
    worker ! Command("ls") 
  } 

  case class Command(cmd: String) 

  class Worker extends Actor { 

    def receive = { 
      case Command(cmd) => { 
        println(cmd) 
        "echo recieve message from someone" ! 
        execute(cmd.toString) 
      } 
    } 

    def execute(cmd: String) { 
      val process = Process(cmd.toString) 
      process ! ProcessLogger(_ => {}) 
    } 
  } 

}
4

1 回答 1

6

它被解释execute(cmd.toString)为 的参数!,因为换行符不一定结束语句。要解决此问题,请不要使用后缀语法,该语法已被弃用,原因如下:

def receive = { 
  case Command(cmd) => { 
    println(cmd) 
    "echo recieve message from someone".! 
    execute(cmd.toString) 
  } 
}
于 2013-10-31T04:28:18.987 回答