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:

    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
  • 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("https://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
  • object ProcessBuilder extends ProcessBuilderImpl

    This object contains traits used to describe input and output sources.

    This object contains traits used to describe input and output sources.

    Definition Classes
    process
  • FileBuilder
  • Sink
  • Source
  • URLBuilder

trait URLBuilder extends Source

Used when creating scala.sys.process.ProcessBuilder.Source from an URL.

Source
ProcessBuilder.scala
Linear Supertypes
Type Hierarchy
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. URLBuilder
  2. Source
  3. AnyRef
  4. Any
Implicitly
  1. by any2stringadd
  2. by StringFormat
  3. by Ensuring
  4. by ArrowAssoc
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. Protected

Abstract Value Members

  1. abstract def toSource: ProcessBuilder
    Attributes
    protected
    Definition Classes
    Source

Concrete 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. def #>(b: ProcessBuilder): ProcessBuilder

    Writes the output stream of this process to a scala.sys.process.ProcessBuilder.

    Writes the output stream of this process to a scala.sys.process.ProcessBuilder.

    Definition Classes
    Source
  4. def #>(out: => OutputStream): ProcessBuilder

    Writes the output stream of this process to the given OutputStream.

    Writes the output stream of this process to the given OutputStream. The argument is call-by-name, so the stream is recreated, written, and closed each time this process is executed.

    Definition Classes
    Source
  5. def #>(f: File): ProcessBuilder

    Writes the output stream of this process to the given file.

    Writes the output stream of this process to the given file.

    Definition Classes
    Source
  6. def #>>(f: File): ProcessBuilder

    Appends the output stream of this process to the given file.

    Appends the output stream of this process to the given file.

    Definition Classes
    Source
  7. def +(other: String): String
    Implicit
    This member is added by an implicit conversion from URLBuilder toany2stringadd[URLBuilder] performed by method any2stringadd in scala.Predef.
    Definition Classes
    any2stringadd
  8. def ->[B](y: B): (URLBuilder, B)
    Implicit
    This member is added by an implicit conversion from URLBuilder toArrowAssoc[URLBuilder] performed by method ArrowAssoc in scala.Predef.
    Definition Classes
    ArrowAssoc
    Annotations
    @inline()
  9. 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
  10. 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.

  11. def cat: ProcessBuilder

    Returns a scala.sys.process.ProcessBuilder representing this Source.

    Returns a scala.sys.process.ProcessBuilder representing this Source.

    Definition Classes
    Source
  12. 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[lang]
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.CloneNotSupportedException]) @native()
    Note

    not specified by SLS as a member of AnyRef

  13. def ensuring(cond: (URLBuilder) => Boolean, msg: => Any): URLBuilder
    Implicit
    This member is added by an implicit conversion from URLBuilder toEnsuring[URLBuilder] performed by method Ensuring in scala.Predef.
    Definition Classes
    Ensuring
  14. def ensuring(cond: (URLBuilder) => Boolean): URLBuilder
    Implicit
    This member is added by an implicit conversion from URLBuilder toEnsuring[URLBuilder] performed by method Ensuring in scala.Predef.
    Definition Classes
    Ensuring
  15. def ensuring(cond: Boolean, msg: => Any): URLBuilder
    Implicit
    This member is added by an implicit conversion from URLBuilder toEnsuring[URLBuilder] performed by method Ensuring in scala.Predef.
    Definition Classes
    Ensuring
  16. def ensuring(cond: Boolean): URLBuilder
    Implicit
    This member is added by an implicit conversion from URLBuilder toEnsuring[URLBuilder] performed by method Ensuring in scala.Predef.
    Definition Classes
    Ensuring
  17. 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
  18. 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
  19. 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[lang]
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.Throwable])
    Note

    not specified by SLS as a member of AnyRef

  20. 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()
  21. 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()
  22. final def isInstanceOf[T0]: Boolean

    Test whether the dynamic type of the receiver object has the same erasure as T0.

    Test whether the dynamic type of the receiver object has the same erasure as T0.

    Depending on what T0 is, the test is done in one of the below ways:

    • T0 is a non-parameterized class type, e.g. BigDecimal: this method returns true if the value of the receiver object is a BigDecimal or a subtype of BigDecimal.
    • T0 is a parameterized class type, e.g. List[Int]: this method returns true if the value of the receiver object is some List[X] for any X. For example, List(1, 2, 3).isInstanceOf[List[String]] will return true.
    • T0 is some singleton type x.type or literal x: this method returns this.eq(x). For example, x.isInstanceOf[1] is equivalent to x.eq(1)
    • T0 is an intersection X with Y or X & Y: this method is equivalent to x.isInstanceOf[X] && x.isInstanceOf[Y]
    • T0 is a union X | Y: this method is equivalent to x.isInstanceOf[X] || x.isInstanceOf[Y]
    • T0 is a type parameter or an abstract type member: this method is equivalent to isInstanceOf[U] where U is T0's upper bound, Any if T0 is unbounded. For example, x.isInstanceOf[A] where A is an unbounded type parameter will return true for any value of x.

    This is exactly equivalent to the type pattern _: T0

    returns

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

    Definition Classes
    Any
    Note

    due to the unexpectedness of List(1, 2, 3).isInstanceOf[List[String]] returning true and x.isInstanceOf[A] where A is a type parameter or abstract member returning true, these forms issue a warning.

  23. 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
  24. 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

  25. 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

  26. final def synchronized[T0](arg0: => T0): T0

    Executes the code in body with an exclusive lock on this.

    Executes the code in body with an exclusive lock on this.

    returns

    the result of body

    Definition Classes
    AnyRef
  27. 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
  28. final def wait(): Unit

    See https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#wait--.

    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.InterruptedException])
    Note

    not specified by SLS as a member of AnyRef

  29. final def wait(arg0: Long, arg1: Int): Unit

    See https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#wait-long-int-

    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.InterruptedException])
    Note

    not specified by SLS as a member of AnyRef

  30. final def wait(arg0: Long): Unit

    See https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#wait-long-.

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

    not specified by SLS as a member of AnyRef

Deprecated Value Members

  1. def formatted(fmtstr: String): String

    Returns string formatted according to given format string.

    Returns string formatted according to given format string. Format strings are as for String.format (@see java.lang.String.format).

    Implicit
    This member is added by an implicit conversion from URLBuilder toStringFormat[URLBuilder] performed by method StringFormat in scala.Predef.
    Definition Classes
    StringFormat
    Annotations
    @deprecated @inline()
    Deprecated

    (Since version 2.12.16) Use formatString.format(value) instead of value.formatted(formatString), or use the f"" string interpolator. In Java 15 and later, formatted resolves to the new method in String which has reversed parameters.

  2. def [B](y: B): (URLBuilder, B)
    Implicit
    This member is added by an implicit conversion from URLBuilder toArrowAssoc[URLBuilder] performed by method ArrowAssoc in scala.Predef.
    Definition Classes
    ArrowAssoc
    Annotations
    @deprecated
    Deprecated

    (Since version 2.13.0) Use -> instead. If you still wish to display it as one character, consider using a font with programming ligatures such as Fira Code.

Inherited from Source

Inherited from AnyRef

Inherited from Any

Inherited by implicit conversion any2stringadd fromURLBuilder to any2stringadd[URLBuilder]

Inherited by implicit conversion StringFormat fromURLBuilder to StringFormat[URLBuilder]

Inherited by implicit conversion Ensuring fromURLBuilder to Ensuring[URLBuilder]

Inherited by implicit conversion ArrowAssoc fromURLBuilder to ArrowAssoc[URLBuilder]

Ungrouped