Scala Standard Library2.13.15 < 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 annotation
  Definition Classes
    scala
-  package beans
  Definition Classes
    scala
-  package collection
  Definition Classes
    scala
-  package compat
  Definition Classes
    scala
-  package concurrent

  This package object contains primitives for concurrent and parallel programming.

  This package object contains primitives for concurrent and parallel programming.

  #### Guide

  A more detailed guide to Futures and Promises, including discussion and examples can be found at https://docs.scala-lang.org/overviews/core/futures.html.

  #### Common Imports

  When working with Futures, you will often find that importing the whole concurrent package is convenient:

      import scala.concurrent._

  When using things like `Future`s, it is often required to have an implicit `ExecutionContext` in scope. The general advice for these implicits are as follows.

  If the code in question is a class or method definition, and no `ExecutionContext` is available, request one from the caller by adding an implicit parameter list:

      def myMethod(myParam: MyType)(implicit ec: ExecutionContext) = …
      //Or
      class MyClass(myParam: MyType)(implicit ec: ExecutionContext) { … }

  This allows the caller of the method, or creator of the instance of the class, to decide which `ExecutionContext` should be used.

  For typical REPL usage and experimentation, importing the global `ExecutionContext` is often desired.

      import scala.concurrent.ExcutionContext.Implicits.global

  #### Specifying Durations

  Operations often require a duration to be specified. A duration DSL is available to make defining these easier:

      import scala.concurrent.duration._
      val d: Duration = 10.seconds

  #### Using Futures For Non-blocking Computation

  Basic use of futures is easy with the factory method on Future, which executes a provided function asynchronously, handing you back a future result of that function without blocking the current thread. In order to create the Future you will need either an implicit or explicit ExecutionContext to be provided:

      import scala.concurrent._
      import ExecutionContext.Implicits.global  // implicit execution context

      val firstZebra: Future[Int] = Future {
        val words = Files.readAllLines("/etc/dictionaries-common/words").asScala
        words.indexOfSlice("zebra")
      }

  #### Avoid Blocking

  Although blocking is possible in order to await results (with a mandatory timeout duration):

      import scala.concurrent.duration._
      Await.result(firstZebra, 10.seconds)

  and although this is sometimes necessary to do, in particular for testing purposes, blocking in general is discouraged when working with Futures and concurrency in order to avoid potential deadlocks and improve performance. Instead, use callbacks or combinators to remain in the future domain:

      val animalRange: Future[Int] = for {
        aardvark <- firstAardvark
        zebra <- firstZebra
      } yield zebra - aardvark

      animalRange.onSuccess {
        case x if x > 500000 => println("It's a long way from Aardvark to Zebra")
      }

  Definition Classes
    scala
-  package io
  Definition Classes
    scala
-  package jdk

  The jdk package contains utilities to interact with JDK classes.

  The jdk package contains utilities to interact with JDK classes.

  This packages offers a number of converters, that are able to wrap or copy types from the scala library to equivalent types in the JDK class library and vice versa:

  - CollectionConverters, converting collections like scala.collection.Seq, scala.collection.Map, scala.collection.Set, scala.collection.mutable.Buffer, scala.collection.Iterator and scala.collection.Iterable to their JDK counterparts
  - OptionConverters, converting between Option and java.util.Optional and primitive variations
  - StreamConverters, to create JDK Streams from scala collections
  - DurationConverters, for conversions between scala scala.concurrent.duration.FiniteDuration and java.time.Duration
  - FunctionConverters, from scala Functions to java java.util.function.Function, java.util.function.UnaryOperator, java.util.function.Consumer and java.util.function.Predicate, as well as primitive variations and Bi-variations.

  By convention, converters that wrap an object to provide a different interface to the same underlying data structure use .asScala and .asJava extension methods, whereas converters that copy the underlying data structure use .toScala and .toJava.

  In the javaapi package, the same converters can be found with a java-friendly interface that don't rely on implicit enrichments.

  Additionally, this package offers Accumulators, capable of efficiently traversing JDK Streams.

  Definition Classes
    scala
-  package math

  The package object `scala.math` contains methods for performing basic numeric operations such as elementary exponential, logarithmic, root and trigonometric functions.

  The package object `scala.math` contains methods for performing basic numeric operations such as elementary exponential, logarithmic, root and trigonometric functions.

  All methods forward to java.lang.Math unless otherwise noted.

  Definition Classes
    scala
  See also

    java.lang.Math

-  package ref
  Definition Classes
    scala
-  package reflect
  Definition Classes
    scala
- AnyValManifest
- ClassManifestDeprecatedApis
- ClassManifestFactory
- ClassTag
- Manifest
- ManifestFactory
- NameTransformer
- NoManifest
- OptManifest
-  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 util
  Definition Classes
    scala

p

scala

# reflect

#### package reflect

Source
  package.scala

Linear Supertypes
AnyRef, Any
Content Hierarchy

Ordering

1. Alphabetic
2. By Inheritance

Inherited

1. reflect
2. AnyRef
3. Any

1. Hide All
2. Show All

Visibility

1. Public
2. Protected

### Type Members

1.  abstract class AnyValManifest[T <: AnyVal] extends Manifest[T] with Equals
   Annotations
     @nowarn() @SerialVersionUID()
2.  trait ClassTag[T] extends ClassManifestDeprecatedApis[T] with Equals with Serializable

   A `ClassTag[T]` stores the erased class of a given type `T`, accessible via the `runtimeClass` field.

   A `ClassTag[T]` stores the erased class of a given type `T`, accessible via the `runtimeClass` field. This is particularly useful for instantiating `Array`s whose element types are unknown at compile time.

   `ClassTag`s are a weaker special case of scala.reflect.api.TypeTags.TypeTags, in that they wrap only the runtime class of a given type, whereas a `TypeTag` contains all static type information. That is, `ClassTag`s are constructed from knowing only the top-level class of a type, without necessarily knowing all of its argument types. This runtime information is enough for runtime `Array` creation.

   For example:

       scala> def mkArray[T : ClassTag](elems: T*) = Array[T](elems: _*)
       mkArray: [T](elems: T*)(implicit evidence$1: scala.reflect.ClassTag[T])Array[T]

       scala> mkArray(42, 13)
       res0: Array[Int] = Array(42, 13)

       scala> mkArray("Japan","Brazil","Germany")
       res1: Array[String] = Array(Japan, Brazil, Germany)

   See scala.reflect.api.TypeTags for more examples, or the Reflection Guide: TypeTags for more details.

   Annotations
     @nowarn() @implicitNotFound()
3.  trait Manifest[T] extends ClassManifest[T] with Equals

   A `Manifest[T]` is an opaque descriptor for type T.

   A `Manifest[T]` is an opaque descriptor for type T. Its supported use is to give access to the erasure of the type as a `Class` instance, as is necessary for the creation of native `Arrays` if the class is not known at compile time.

   The type-relation operators `<:<` and `=:=` should be considered approximations only, as there are numerous aspects of type conformance which are not yet adequately represented in manifests.

   Example usages:

       def arr[T] = new Array[T](0)                          // does not compile
       def arr[T](implicit m: Manifest[T]) = new Array[T](0) // compiles
       def arr[T: Manifest] = new Array[T](0)                // shorthand for the preceding

       // Methods manifest and optManifest are in [[scala.Predef]].
       def isApproxSubType[T: Manifest, U: Manifest] = manifest[T] <:< manifest[U]
       isApproxSubType[List[String], List[AnyRef]] // true
       isApproxSubType[List[String], List[Int]]    // false

       def methods[T: Manifest] = manifest[T].runtimeClass.getMethods
       def retType[T: Manifest](name: String) =
         methods[T] find (_.getName == name) map (_.getGenericReturnType)

       retType[Map[_, _]]("values")  // Some(scala.collection.Iterable<B>)

   Annotations
     @nowarn() @implicitNotFound()
4.  trait OptManifest[+T] extends Serializable

   A `OptManifest[T]` is an optional scala.reflect.Manifest.

   A `OptManifest[T]` is an optional scala.reflect.Manifest.

   It is either a `Manifest` or the value `NoManifest`.

### Deprecated Type Members

1.  type ClassManifest[T] = ClassTag[T]

   A `ClassManifest[T]` is an opaque descriptor for type `T`.

   A `ClassManifest[T]` is an opaque descriptor for type `T`. It is used by the compiler to preserve information necessary for instantiating `Arrays` in those cases where the element type is unknown at compile time.

   The type-relation operators make an effort to present a more accurate picture than can be realized with erased types, but they should not be relied upon to give correct answers. In particular they are likely to be wrong when variance is involved or when a subtype has a different number of type arguments than a supertype.

   Annotations
     @deprecated @implicitNotFound()
   Deprecated

     (Since version 2.10.0) use scala.reflect.ClassTag instead

2.  trait ClassManifestDeprecatedApis[T] extends OptManifest[T]
   Annotations
     @deprecated
   Deprecated

     (Since version 2.10.0) use scala.reflect.ClassTag instead

### Value Members

1.  def classTag[T](implicit ctag: ClassTag[T]): ClassTag[T]
2.  def ensureAccessible[T <: AccessibleObject](m: T): T

   Make a java reflection object accessible, if it is not already and it is possible to do so.

   Make a java reflection object accessible, if it is not already and it is possible to do so. If a SecurityException is thrown in the attempt, it is caught and discarded.

3.  object ClassManifestFactory

   `ClassManifestFactory` defines factory methods for manifests.

   `ClassManifestFactory` defines factory methods for manifests. It is intended for use by the compiler and should not be used in client code.

   Unlike `ClassManifest`, this factory isn't annotated with a deprecation warning. This is done to prevent avalanches of deprecation warnings in the code that calls methods with manifests.

   In a perfect world, we would just remove the @deprecated annotation from `ClassManifest` the object and then delete it in 2.11. After all, that object is explicitly marked as internal, so no one should use it. However a lot of existing libraries disregarded the Scaladoc that comes with `ClassManifest`, so we need to somehow nudge them into migrating prior to removing stuff out of the blue. Hence we've introduced this design decision as the lesser of two evils.

   Annotations
     @nowarn()
4.  object ClassTag extends java.io.Serializable

   Class tags corresponding to primitive types and constructor/extractor for ClassTags.

5.  object Manifest extends java.io.Serializable

   The object `Manifest` defines factory methods for manifests.

   The object `Manifest` defines factory methods for manifests. It is intended for use by the compiler and should not be used in client code.

6.  object ManifestFactory

   `ManifestFactory` defines factory methods for manifests.

   `ManifestFactory` defines factory methods for manifests. It is intended for use by the compiler and should not be used in client code.

   Unlike `Manifest`, this factory isn't annotated with a deprecation warning. This is done to prevent avalanches of deprecation warnings in the code that calls methods with manifests. Why so complicated? Read up the comments for `ClassManifestFactory`.

   Annotations
     @nowarn()
7.  object NameTransformer

   Provides functions to encode and decode Scala symbolic names.

   Provides functions to encode and decode Scala symbolic names. Also provides some constants.

8.  object NoManifest extends OptManifest[Nothing] with Serializable

   One of the branches of an scala.reflect.OptManifest.

### Deprecated Value Members

1.  val ClassManifest: ClassManifestFactory.type

   The object `ClassManifest` defines factory methods for manifests.

   The object `ClassManifest` defines factory methods for manifests. It is intended for use by the compiler and should not be used in client code.

   Annotations
     @deprecated
   Deprecated

     (Since version 2.10.0) use scala.reflect.ClassTag instead

### Inherited from AnyRef

### Inherited from Any

### Ungrouped