package compat
- Alphabetic
- Public
- All
This is the documentation for the Scala standard library.
Core Scala types.
Core Scala types. They are always available without an explicit import.
Contains the base traits and objects needed to use and extend Scala's collection library.
Contains the base traits and objects needed to use and extend Scala's collection library.
A detailed guide for using the collections library is available at http://docs.scala-lang.org/overviews/collections/introduction.html. Developers looking to extend the collections library can find a description of its architecture at http://docs.scala-lang.org/overviews/core/architecture-of-scala-collections.html.
It is convenient to treat all collections as either a scala.collection.Traversable or scala.collection.Iterable, as these traits define the vast majority of operations on a collection.
Collections can, of course, be treated as specifically as needed, and the library is designed to ensure that the methods that transform collections will return a collection of the same type:
scala> val array = Array(1,2,3,4,5,6) array: Array[Int] = Array(1, 2, 3, 4, 5, 6) scala> array map { _.toString } res0: Array[String] = Array(1, 2, 3, 4, 5, 6) scala> val list = List(1,2,3,4,5,6) list: List[Int] = List(1, 2, 3, 4, 5, 6) scala> list map { _.toString } res1: List[String] = List(1, 2, 3, 4, 5, 6)
The most common way to create a collection is to use its companion object as
a factory. The three most commonly used collections are
scala.collection.Seq, scala.collection.immutable.Set, and
scala.collection.immutable.Map.
They can be used directly as shown below since their companion objects are
all available as type aliases in either the scala package or in
scala.Predef
. New collections are created like this:
scala> val seq = Seq(1,2,3,4,1) seq: Seq[Int] = List(1, 2, 3, 4, 1) scala> val set = Set(1,2,3,4,1) set: scala.collection.immutable.Set[Int] = Set(1, 2, 3, 4) scala> val map = Map(1 -> "one", 2 -> "two", 3 -> "three", 2 -> "too") map: scala.collection.immutable.Map[Int,String] = Map(1 -> one, 2 -> too, 3 -> three)
It is also typical to prefer the scala.collection.immutable collections
over those in scala.collection.mutable; the types aliased in
the scala.Predef
object are the immutable versions.
Also note that the collections library was carefully designed to include several implementations of each of the three basic collection types. These implementations have specific performance characteristics which are described in the guide.
The concrete parallel collections also have specific performance characteristics which are described in the parallel collections guide
The scala.collection.JavaConverters object provides a collection
of decorators that allow converting between Scala and Java collections using asScala
and asJava
methods.
This package object contains primitives for concurrent and parallel programming.
This package object contains primitives for concurrent and parallel programming.
A more detailed guide to Futures and Promises, including discussion and examples can be found at http://docs.scala-lang.org/overviews/core/futures.html.
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
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
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 source = scala.io.Source.fromFile("/etc/dictionaries-common/words") source.toSeq.indexOfSlice("zebra") }
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") }
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.
java.lang.Math
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.
2.9
2.9
This is the documentation for the Scala standard library.
Package structure
The scala package contains core types like
Int
,Float
,Array
orOption
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 frameworkscala.collection.immutable
- Immutable, sequential data-structures such asVector
,List
,Range
,HashMap
orHashSet
scala.collection.mutable
- Mutable, sequential data-structures such asArrayBuffer
,StringBuilder
,HashMap
orHashSet
scala.collection.concurrent
- Mutable, concurrent data-structures such asTrieMap
scala.collection.parallel.immutable
- Immutable, parallel data-structures such asParVector
,ParRange
,ParHashMap
orParHashSet
scala.collection.parallel.mutable
- Mutable, parallel data-structures such asParArray
,ParHashMap
,ParTrieMap
orParHashSet
scala.concurrent
- Primitives for concurrent programming such asFutures
andPromises
scala.io
- Input and output operationsscala.math
- Basic math functions and additional numeric types likeBigInt
andBigDecimal
scala.sys
- Interaction with other processes and the operating systemscala.util.matching
- Regular expressionsOther 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.swing
- A convenient wrapper around Java's GUI framework called Swing (scala-swing.jar)scala.util.parsing
- Parser combinators (scala-parser-combinators.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 forscala.collection.immutable.List
.Other aliases refer to classes provided by the underlying platform. For example, on the JVM,
String
is an alias forjava.lang.String
.