Packages

  • package root

    This is the documentation for the Scala standard library.

    This is the documentation for the Scala standard library.

    Package structure

    The scala package contains core types like Int, Float, Array or Option which are accessible in all Scala compilation units without explicit qualification or imports.

    Notable packages include:

    Other packages exist. See the complete list on the right.

    Additional parts of the standard library are shipped as separate libraries. These include:

    • scala.reflect - Scala's reflection API (scala-reflect.jar)
    • scala.xml - XML parsing, manipulation, and serialization (scala-xml.jar)
    • scala.collection.parallel - Parallel collections (scala-parallel-collections.jar)
    • scala.util.parsing - Parser combinators (scala-parser-combinators.jar)
    • scala.swing - A convenient wrapper around Java's GUI framework called Swing (scala-swing.jar)

    Automatic imports

    Identifiers in the scala package and the scala.Predef object are always in scope by default.

    Some of these identifiers are type aliases provided as shortcuts to commonly used classes. For example, List is an alias for scala.collection.immutable.List.

    Other aliases refer to classes provided by the underlying platform. For example, on the JVM, String is an alias for java.lang.String.

    Definition Classes
    root
  • package scala

    Core Scala types.

    Core Scala types. They are always available without an explicit import.

    Definition Classes
    root
  • package sys

    The package object scala.sys contains methods for reading and altering core aspects of the virtual machine as well as the world outside of it.

    The package object scala.sys contains methods for reading and altering core aspects of the virtual machine as well as the world outside of it.

    Definition Classes
    scala
    Since

    2.9

  • package process

    This package handles the execution of external processes.

    This package handles the execution of external processes. The contents of this package can be divided in three groups, according to their responsibilities:

    • Indicating what to run and how to run it.
    • Handling a process input and output.
    • Running the process.

    For simple uses, the only group that matters is the first one. Running an external command can be as simple as "ls".!, or as complex as building a pipeline of commands such as this:

    import scala.sys.process._
    "ls" #| "grep .scala" #&& Seq("sh", "-c", "scalac *.scala") #|| "echo nothing found" lazyLines

    We describe below the general concepts and architecture of the package, and then take a closer look at each of the categories mentioned above.

    Concepts and Architecture

    The underlying basis for the whole package is Java's Process and ProcessBuilder classes. While there's no need to use these Java classes, they impose boundaries on what is possible. One cannot, for instance, retrieve a process id for whatever is executing.

    When executing an external process, one can provide a command's name, arguments to it, the directory in which it will be executed and what environment variables will be set. For each executing process, one can feed its standard input through a java.io.OutputStream, and read from its standard output and standard error through a pair of java.io.InputStream. One can wait until a process finishes execution and then retrieve its return value, or one can kill an executing process. Everything else must be built on those features.

    This package provides a DSL for running and chaining such processes, mimicking Unix shells ability to pipe output from one process to the input of another, or control the execution of further processes based on the return status of the previous one.

    In addition to this DSL, this package also provides a few ways of controlling input and output of these processes, going from simple and easy to use to complex and flexible.

    When processes are composed, a new ProcessBuilder is created which, when run, will execute the ProcessBuilder instances it is composed of according to the manner of the composition. If piping one process to another, they'll be executed simultaneously, and each will be passed a ProcessIO that will copy the output of one to the input of the other.

    What to Run and How

    The central component of the process execution DSL is the scala.sys.process.ProcessBuilder trait. It is ProcessBuilder that implements the process execution DSL, that creates the scala.sys.process.Process that will handle the execution, and return the results of such execution to the caller. We can see that DSL in the introductory example: #|, #&& and #!! are methods on ProcessBuilder used to create a new ProcessBuilder through composition.

    One creates a ProcessBuilder either through factories on the scala.sys.process.Process's companion object, or through implicit conversions available in this package object itself. Implicitly, each process is created either out of a String, with arguments separated by spaces -- no escaping of spaces is possible -- or out of a scala.collection.Seq, where the first element represents the command name, and the remaining elements are arguments to it. In this latter case, arguments may contain spaces.

    To further control what how the process will be run, such as specifying the directory in which it will be run, see the factories on scala.sys.process.Process's companion object.

    Once the desired ProcessBuilder is available, it can be executed in different ways, depending on how one desires to control its I/O, and what kind of result one wishes for:

    • Return status of the process (! methods)
    • Output of the process as a String (!! methods)
    • Continuous output of the process as a LazyList[String] (lazyLines methods)
    • The Process representing it (run methods)

    Some simple examples of these methods:

    import scala.sys.process._
    
    // This uses ! to get the exit code
    def fileExists(name: String) = Seq("test", "-f", name).! == 0
    
    // This uses !! to get the whole result as a string
    val dirContents = "ls".!!
    
    // This "fire-and-forgets" the method, which can be lazily read through
    // a LazyList[String]
    def sourceFilesAt(baseDir: String): LazyList[String] = {
      val cmd = Seq("find", baseDir, "-name", "*.scala", "-type", "f")
      cmd.lazyLines
    }

    We'll see more details about controlling I/O of the process in the next section.

    Handling Input and Output

    In the underlying Java model, once a Process has been started, one can get java.io.InputStream and java.io.OutputStream representing its output and input respectively. That is, what one writes to an OutputStream is turned into input to the process, and the output of a process can be read from an InputStream -- of which there are two, one representing normal output, and the other representing error output.

    This model creates a difficulty, which is that the code responsible for actually running the external processes is the one that has to take decisions about how to handle its I/O.

    This package presents an alternative model: the I/O of a running process is controlled by a scala.sys.process.ProcessIO object, which can be passed _to_ the code that runs the external process. A ProcessIO will have direct access to the java streams associated with the process I/O. It must, however, close these streams afterwards.

    Simpler abstractions are available, however. The components of this package that handle I/O are:

    Some examples of I/O handling:

    import scala.sys.process._
    
    // An overly complex way of computing size of a compressed file
    def gzFileSize(name: String) = {
      val cat = Seq("zcat", name)
      var count = 0
      def byteCounter(input: java.io.InputStream) = {
        while(input.read() != -1) count += 1
        input.close()
      }
      val p = cat run new ProcessIO(_.close(), byteCounter, _.close())
      p.exitValue()
      count
    }
    
    // This "fire-and-forgets" the method, which can be lazily read through
    // a LazyList[String], and accumulates all errors on a StringBuffer
    def sourceFilesAt(baseDir: String): (LazyList[String], StringBuffer) = {
      val buffer = new StringBuffer()
      val cmd = Seq("find", baseDir, "-name", "*.scala", "-type", "f")
      val lazyLines = cmd lazyLines_! ProcessLogger(buffer append _)
      (lazyLines, buffer)
    }

    Instances of the java classes java.io.File and java.net.URL can both be used directly as input to other processes, and java.io.File can be used as output as well. One can even pipe one to the other directly without any intervening process, though that's not a design goal or recommended usage. For example, the following code will copy a web page to a file:

    import java.io.File
    import java.net.URL
    import scala.sys.process._
    new URL("http://www.scala-lang.org/") #> new File("scala-lang.html") !

    More information about the other ways of controlling I/O can be found in the Scaladoc for the associated objects, traits and classes.

    Running the Process

    Paradoxically, this is the simplest component of all, and the one least likely to be interacted with. It consists solely of scala.sys.process.Process, and it provides only two methods:

    • exitValue(): blocks until the process exit, and then returns the exit value. This is what happens when one uses the ! method of ProcessBuilder.
    • destroy(): this will kill the external process and close the streams associated with it.
    Definition Classes
    sys
  • BooleanProp
  • Prop
  • ShutdownHookThread
  • SystemProperties

object ShutdownHookThread

Source
ShutdownHookThread.scala
Linear Supertypes
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. ShutdownHookThread
  2. AnyRef
  3. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. All

Value Members

  1. final def !=(arg0: Any): Boolean

    Test two objects for inequality.

    Test two objects for inequality.

    returns

    true if !(this == that), false otherwise.

    Definition Classes
    AnyRef → Any
  2. final def ##(): Int

    Equivalent to x.hashCode except for boxed numeric types and null.

    Equivalent to x.hashCode except for boxed numeric types and null. For numerics, it returns a hash value which is consistent with value equality: if two value type instances compare as true, then ## will produce the same hash value for each of them. For null returns a hashcode where null.hashCode throws a NullPointerException.

    returns

    a hash value consistent with ==

    Definition Classes
    AnyRef → Any
  3. final def ==(arg0: Any): Boolean

    The expression x == that is equivalent to if (x eq null) that eq null else x.equals(that).

    The expression x == that is equivalent to if (x eq null) that eq null else x.equals(that).

    returns

    true if the receiver object is equivalent to the argument; false otherwise.

    Definition Classes
    AnyRef → Any
  4. def apply(body: => Unit): ShutdownHookThread

    Creates, names, and registers a shutdown hook to run the given code.

  5. final def asInstanceOf[T0]: T0

    Cast the receiver object to be of type T0.

    Cast the receiver object to be of type T0.

    Note that the success of a cast at runtime is modulo Scala's erasure semantics. Therefore the expression 1.asInstanceOf[String] will throw a ClassCastException at runtime, while the expression List(1).asInstanceOf[List[String]] will not. In the latter example, because the type argument is erased as part of compilation it is not possible to check whether the contents of the list are of the requested type.

    returns

    the receiver object.

    Definition Classes
    Any
    Exceptions thrown

    ClassCastException if the receiver object is not an instance of the erasure of type T0.

  6. def clone(): AnyRef

    Create a copy of the receiver object.

    Create a copy of the receiver object.

    The default implementation of the clone method is platform dependent.

    returns

    a copy of the receiver object.

    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.CloneNotSupportedException]) @native()
    Note

    not specified by SLS as a member of AnyRef

  7. final def eq(arg0: AnyRef): Boolean

    Tests whether the argument (that) is a reference to the receiver object (this).

    Tests whether the argument (that) is a reference to the receiver object (this).

    The eq method implements an equivalence relation on non-null instances of AnyRef, and has three additional properties:

    • It is consistent: for any non-null instances x and y of type AnyRef, multiple invocations of x.eq(y) consistently returns true or consistently returns false.
    • For any non-null instance x of type AnyRef, x.eq(null) and null.eq(x) returns false.
    • null.eq(null) returns true.

    When overriding the equals or hashCode methods, it is important to ensure that their behavior is consistent with reference equality. Therefore, if two objects are references to each other (o1 eq o2), they should be equal to each other (o1 == o2) and they should hash to the same value (o1.hashCode == o2.hashCode).

    returns

    true if the argument is a reference to the receiver object; false otherwise.

    Definition Classes
    AnyRef
  8. def equals(arg0: AnyRef): Boolean

    The equality method for reference types.

    The equality method for reference types. Default implementation delegates to eq.

    See also equals in scala.Any.

    returns

    true if the receiver object is equivalent to the argument; false otherwise.

    Definition Classes
    AnyRef → Any
  9. def finalize(): Unit

    Called by the garbage collector on the receiver object when there are no more references to the object.

    Called by the garbage collector on the receiver object when there are no more references to the object.

    The details of when and if the finalize method is invoked, as well as the interaction between finalize and non-local returns and exceptions, are all platform dependent.

    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.Throwable])
    Note

    not specified by SLS as a member of AnyRef

  10. final def getClass(): Class[_ <: AnyRef]

    Returns the runtime class representation of the object.

    Returns the runtime class representation of the object.

    returns

    a class object corresponding to the runtime type of the receiver.

    Definition Classes
    AnyRef → Any
    Annotations
    @native()
  11. def hashCode(): Int

    The hashCode method for reference types.

    The hashCode method for reference types. See hashCode in scala.Any.

    returns

    the hash code value for this object.

    Definition Classes
    AnyRef → Any
    Annotations
    @native()
  12. final def isInstanceOf[T0]: Boolean

    Test whether the dynamic type of the receiver object is T0.

    Test whether the dynamic type of the receiver object is T0.

    Note that the result of the test is modulo Scala's erasure semantics. Therefore the expression 1.isInstanceOf[String] will return false, while the expression List(1).isInstanceOf[List[String]] will return true. In the latter example, because the type argument is erased as part of compilation it is not possible to check whether the contents of the list are of the specified type.

    returns

    true if the receiver object is an instance of erasure of type T0; false otherwise.

    Definition Classes
    Any
  13. final def ne(arg0: AnyRef): Boolean

    Equivalent to !(this eq that).

    Equivalent to !(this eq that).

    returns

    true if the argument is not a reference to the receiver object; false otherwise.

    Definition Classes
    AnyRef
  14. final def notify(): Unit

    Wakes up a single thread that is waiting on the receiver object's monitor.

    Wakes up a single thread that is waiting on the receiver object's monitor.

    Definition Classes
    AnyRef
    Annotations
    @native()
    Note

    not specified by SLS as a member of AnyRef

  15. final def notifyAll(): Unit

    Wakes up all threads that are waiting on the receiver object's monitor.

    Wakes up all threads that are waiting on the receiver object's monitor.

    Definition Classes
    AnyRef
    Annotations
    @native()
    Note

    not specified by SLS as a member of AnyRef

  16. final def synchronized[T0](arg0: => T0): T0
    Definition Classes
    AnyRef
  17. def toString(): String

    Creates a String representation of this object.

    Creates a String representation of this object. The default representation is platform dependent. On the java platform it is the concatenation of the class name, "@", and the object's hashcode in hexadecimal.

    returns

    a String representation of the object.

    Definition Classes
    AnyRef → Any
  18. final def wait(): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.InterruptedException])
  19. final def wait(arg0: Long, arg1: Int): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.InterruptedException])
  20. final def wait(arg0: Long): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.InterruptedException]) @native()

Inherited from AnyRef

Inherited from Any

Ungrouped