如何在Scala actor中执行(执行)外部系统命令? [英] how to execute (exec) external system commands in Scala actor?
问题描述
要运行外部命令,我应该
to run an external command, I should
import sys.process._
val result="ls a" !
然后在使用actor时,我还需要使用!发送消息。所以!是在actor和过程中都定义的,但是我需要在同一代码块中同时使用它们,该怎么做?
then when use actor, I need to also use "!" to send message. So ! is defined both in actor and in process, but I need to use them both in the same code block, how to do that?
推荐答案
我看不到 Process
和 ActorRef
都定义同名方法的问题。
I don't see the issue of Process
and ActorRef
both defining a method with the same name.
一个类似的例子是
class A { def ! = println("A") }
class B { def ! = println("B") }
val a = new A
val b = new B
a.! // "A"
b.! // "B"
完全没有名称冲突或歧义。
There's no name collision or ambiguity at all.
您唯一需要担心的是从 String
到 Process的隐式转换
。
The only thing you have to worry about is the implicit conversion from String
to Process
.
foo。!
之所以起作用是因为 Process
是唯一可以将 String
隐式转换为定义!
方法的类。
"foo".!
works because Process
is the only class in which String
can implicitly be converted to that defines a !
method.
如文档所述,如果您改用 foo .lines
之类的东西,则编译器会得到混淆,因为它不知道是将 String
转换为 Process
还是转换为 StringLike
,因为两者都定义了行
方法。
As the documentation says, if you instead use something like "foo".lines
, then the compiler gets confuse because it doesn't know whether to convert String
to Process
or to StringLike
, since both define a lines
method.
但是-再次-这不是您的情况,您可以放心地执行以下操作:
But - again - this is not your case, and you can safely do something like:
"ls".!
sender ! "a message"
编译器不应抱怨。
这篇关于如何在Scala actor中执行(执行)外部系统命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!