Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does Some(string.!!) mean in Scala?

Tags:

scala

I have

val str = s"""/bin/bash -c 'some command'"""
job = Some(str.!!)

It is meant to execute the bash command I assume.

Can someone explain this syntax? Googling for '.!!' doesn't help much neither does 'dot exclamation exclamation' so I hope someone can explain this one and/or point me to the doc.

The job doesn't run and I'm trying to debug the code, but when i put this in a

try {
   command = Some(str.!!)
} 
catch { 
case e:Exception => 
  println(e.toString)
}

e is actually not an Exception for some reason... Trying to figure what this really does and how to find what is happening.

like image 873
MrE Avatar asked Mar 09 '16 19:03

MrE


People also ask

What are strings in Scala?

In Scala, as in Java, a string is an immutable object, that is, an object that cannot be modified. On the other hand, objects that can be modified, like arrays, are called mutable objects. Strings are very useful objects, in the rest of this section, we present important methods of java. lang.

What is ${} in Scala?

Any arbitrary expression can be embedded in ${} . For some special characters, it is necessary to escape them when embedded within a string. To represent an actual dollar sign you can double it $$ , like here: Scala 2 and 3.

What is string context?

For many languages, the string "Order" needs a different translation for each of these meanings. In Drupal 7. x and later, it is possible to make different versions of such strings for translation. This is done by adding text to make unique strings for each meaning. This extra text is called string context.


1 Answers

There is an implicit conversion from String to ProcessBuilder. When you import scala.sys.process._ then scala will automatically perform the conversion when needed, thus making the method !! available on String instances. You can find the methods of ProcessBuilder here: http://www.scala-lang.org/api/current/index.html#scala.sys.process.ProcessBuilder

The documentation for !! says that "If the exit code is non-zero, an exception is thrown." It appears that bash in this case does return 0, so the command was for some reason successful.

like image 55
Mark Avatar answered Oct 12 '22 06:10

Mark