gpt4 book ai didi

scala - Akka——类型不匹配; [错误] 发现 : Unit [error] required: scala. sys.process.ProcessLogger

转载 作者:行者123 更新时间:2023-12-04 17:52:49 24 4
gpt4 key购买 nike

我尝试编写示例代码来结合 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(_ => {})
}
}

}

最佳答案

正在翻译execute(cmd.toString)作为 ! 的参数,因为换行不一定结束语句。要解决此问题,请不要使用 postfix 语法,因为它已被弃用:

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

关于scala - Akka——类型不匹配; [错误] 发现 : Unit [error] required: scala. sys.process.ProcessLogger,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19698180/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com