Scala Standard Library2.13.2 < Back


# 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:

  - `scala.collection` and its sub-packages contain Scala's collections framework
    - `scala.collection.immutable` - Immutable, sequential data-structures such as `Vector`, `List`, `Range`, `HashMap` or `HashSet`
    - `scala.collection.mutable` - Mutable, sequential data-structures such as `ArrayBuffer`, `StringBuilder`, `HashMap` or `HashSet`
    - `scala.collection.concurrent` - Mutable, concurrent data-structures such as `TrieMap`
  - `scala.concurrent` - Primitives for concurrent programming such as `Futures` and `Promises`
  - `scala.io` - Input and output operations
  - `scala.math` - Basic math functions and additional numeric types like `BigInt` and `BigDecimal`
  - `scala.sys` - Interaction with other processes and the operating system
  - `scala.util.matching` - Regular expressions

  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
-  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:

  - scala.sys.process.ProcessIO: provides the low level abstraction.
  - scala.sys.process.ProcessLogger: provides a higher level abstraction for output, and can be created through its companion object.
  - scala.sys.process.BasicIO: a library of helper methods for the creation of `ProcessIO`.
  - This package object itself, with a few implicit conversions.

  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

t

scala.sys

# Prop

###
Companion object Prop

#### trait Prop[+T] extends AnyRef

A lightweight interface wrapping a property contained in some unspecified map. Generally it'll be the system properties but this is not a requirement.

See `scala.sys.SystemProperties` for an example usage.

Source
  Prop.scala

Linear Supertypes
AnyRef, Any
Known Subclasses
BooleanProp
Type Hierarchy

Ordering

1. Alphabetic
2. By Inheritance

Inherited

1. Prop
2. AnyRef
3. 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 clear(): Unit

   Removes the property from the underlying map.

2.  abstract def get: String

   Gets the current string value if any.

   Gets the current string value if any. Will not return null: use `isSet` to test for existence.

   returns

     the current string value if any, else the empty string

3.  abstract def isSet: Boolean

   True if the key exists in the properties map.

   True if the key exists in the properties map. Note that this is not sufficient for a Boolean property to be considered true.

   returns

     whether the map contains the key

4.  abstract def key: String

   The full name of the property, e.g., "java.awt.headless".

5.  abstract def option: Option[T]

   Some(value) if the property is set, None otherwise.

6.  abstract def set(newValue: String): String

   Sets the property.

   Sets the property.

   newValue

     the new string value

   returns

     the old value, or null if it was unset.

7.  abstract def setValue[T1 >: T](value: T1): T

   Sets the property with a value of the represented type.

8.  abstract def value: T

   If the key exists in the properties map, converts the value to type `T` using valueFn.

   If the key exists in the properties map, converts the value to type `T` using valueFn. As yet no validation is performed: it will throw an exception on a failed conversion.

   returns

     the converted value, or `zero` if not in the map

9.  abstract def zero: T

   A value of type `T` for use when the property is unset.

   A value of type `T` for use when the property is unset. The default implementation delivers null for reference types and 0/0.0/false for non-reference types.

   Attributes
     protected

### 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 +(other: String): String
   Implicit
     This member is added by an implicit conversion from Prop[T] toany2stringadd[Prop[T]] performed by method any2stringadd in scala.Predef.
   Definition Classes
     any2stringadd
4.  def ->[B](y: B): (Prop[T], B)
   Implicit
     This member is added by an implicit conversion from Prop[T] toArrowAssoc[Prop[T]] performed by method ArrowAssoc in scala.Predef.
   Definition Classes
     ArrowAssoc
   Annotations
     @inline()
5.  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
6.  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`.

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

8.  def ensuring(cond: (Prop[T]) => Boolean, msg: => Any): Prop[T]
   Implicit
     This member is added by an implicit conversion from Prop[T] toEnsuring[Prop[T]] performed by method Ensuring in scala.Predef.
   Definition Classes
     Ensuring
9.  def ensuring(cond: (Prop[T]) => Boolean): Prop[T]
   Implicit
     This member is added by an implicit conversion from Prop[T] toEnsuring[Prop[T]] performed by method Ensuring in scala.Predef.
   Definition Classes
     Ensuring
10.  def ensuring(cond: Boolean, msg: => Any): Prop[T]
    Implicit
      This member is added by an implicit conversion from Prop[T] toEnsuring[Prop[T]] performed by method Ensuring in scala.Predef.
    Definition Classes
      Ensuring
11.  def ensuring(cond: Boolean): Prop[T]
    Implicit
      This member is added by an implicit conversion from Prop[T] toEnsuring[Prop[T]] performed by method Ensuring in scala.Predef.
    Definition Classes
      Ensuring
12.  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
13.  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
14.  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

15.  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 Prop[T] toStringFormat[Prop[T]] performed by method StringFormat in scala.Predef.
    Definition Classes
      StringFormat
    Annotations
      @inline()
16.  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()
17.  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()
18.  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
19.  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
20.  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

21.  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

22.  final def synchronized[T0](arg0: => T0): T0
    Definition Classes
      AnyRef
23.  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
24.  final def wait(): Unit
    Definition Classes
      AnyRef
    Annotations
      @throws(classOf[java.lang.InterruptedException])
25.  final def wait(arg0: Long, arg1: Int): Unit
    Definition Classes
      AnyRef
    Annotations
      @throws(classOf[java.lang.InterruptedException])
26.  final def wait(arg0: Long): Unit
    Definition Classes
      AnyRef
    Annotations
      @throws(classOf[java.lang.InterruptedException]) @native()

### Deprecated Value Members

1.  def →[B](y: B): (Prop[T], B)
   Implicit
     This member is added by an implicit conversion from Prop[T] toArrowAssoc[Prop[T]] 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 AnyRef

### Inherited from Any

### Inherited by implicit conversion any2stringadd fromProp[T] to any2stringadd[Prop[T]]

### Inherited by implicit conversion StringFormat fromProp[T] to StringFormat[Prop[T]]

### Inherited by implicit conversion Ensuring fromProp[T] to Ensuring[Prop[T]]

### Inherited by implicit conversion ArrowAssoc fromProp[T] to ArrowAssoc[Prop[T]]

### Ungrouped