1

to run an external command, I should

import sys.process._
val result="ls a" !

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?

Daniel Wu
  • 5,853
  • 12
  • 42
  • 93

2 Answers2

2

I don't see the issue of Process and ActorRef both defining a method with the same name.

An analog example would be

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.

The only thing you have to worry about is the implicit conversion from String to Process.

"foo".! works because Process is the only class in which String can implicitly be converted to that defines a ! method.

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"

and the compiler should not complain.

Gabriele Petronella
  • 106,943
  • 21
  • 217
  • 235
-1

The recommended approach for such cases seems to be import Process object only:

import scala.sys.process.Process
Process("find src -name *.scala -exec grep null {} ;") #| Process("xargs test -z") #&& Process("echo null-free") #|| Process("echo null detected") !

in your case:

import scala.sys.process.Process
Process("ls a")
Ashalynd
  • 12,363
  • 2
  • 34
  • 37
  • that's a different matter: the docs suggests that approach in case you use a method on `String` that can make the implicit conversion ambiguous (like the deprecated `lines`, which is available on both `Process` and `StringLike`). It doesn't have anything to do with this case. – Gabriele Petronella Aug 16 '14 at 17:41
  • It does, because it allows you not to use ! for performing system commands. The other method would be aliasing '!' somehow. – Ashalynd Aug 16 '14 at 18:15
  • no, it doesn't. The only possible issue is if the method leads to ambiguous implicit conversions, but in that case you get a specific compile-time error. There's such thing as method "aliasing". – Gabriele Petronella Aug 17 '14 at 23:24