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 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 http://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 Futures, 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 source = scala.io.Source.fromFile("/etc/dictionaries-common/words")
      source.toSeq.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 duration
    Definition Classes
    concurrent
  • Deadline
  • DoubleMult
  • Duration
  • DurationConversions
  • DurationDouble
  • DurationInt
  • DurationLong
  • FiniteDuration
  • IntMult
  • LongMult
  • fromNow
  • span

package duration

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

Type Members

  1. case class Deadline extends Ordered[Deadline] with Product with Serializable

    This class stores a deadline, as obtained via Deadline.now or the duration DSL:

    This class stores a deadline, as obtained via Deadline.now or the duration DSL:

    import scala.concurrent.duration._
    3.seconds.fromNow

    Its main purpose is to manage repeated attempts to achieve something (like awaiting a condition) by offering the methods hasTimeLeft and timeLeft. All durations are measured according to System.nanoTime; this does not take into account changes to the system clock (such as leap seconds).

  2. implicit final class DoubleMult extends AnyVal
  3. sealed abstract class Duration extends Serializable with Ordered[Duration]

    This class is not meant as a general purpose representation of time, it is optimized for the needs of scala.concurrent.

    Utility for working with java.util.concurrent.TimeUnit durations.

    This class is not meant as a general purpose representation of time, it is optimized for the needs of scala.concurrent.

    Basic Usage

    Examples:

    import scala.concurrent.duration._
    
    val duration = Duration(100, MILLISECONDS)
    val duration = Duration(100, "millis")
    
    duration.toNanos
    duration < 1.second
    duration <= Duration.Inf

    Invoking inexpressible conversions (like calling toSeconds on an infinite duration) will throw an IllegalArgumentException.

    Implicits are also provided for Int, Long and Double. Example usage:

    import scala.concurrent.duration._
    
    val duration = 100.millis

    The DSL provided by the implicit conversions always allows construction of finite durations, even for infinite Double inputs; use Duration.Inf instead.

    Extractors, parsing and arithmetic are also included:

    val d = Duration("1.2 µs")
    val Duration(length, unit) = 5 millis
    val d2 = d * 2.5
    val d3 = d2 + 1.millisecond

    Handling of Time Units

    Calculations performed on finite durations always retain the more precise unit of either operand, no matter whether a coarser unit would be able to exactly express the same duration. This means that Duration can be used as a lossless container for a (length, unit) pair if it is constructed using the corresponding methods and no arithmetic is performed on it; adding/subtracting durations should in that case be done with care.

    Correspondence to Double Semantics

    The semantics of arithmetic operations on Duration are two-fold:

    • exact addition/subtraction with nanosecond resolution for finite durations, independent of the summands' magnitude
    • isomorphic to java.lang.Double when it comes to infinite or undefined values

    The conversion between Duration and Double is done using Duration.toUnit (with unit NANOSECONDS) and Duration.fromNanos(Double)

    Ordering

    The default ordering is consistent with the ordering of Double numbers, which means that Undefined is considered greater than all other durations, including Duration.Inf.

  4. trait DurationConversions extends Any
  5. implicit final class DurationDouble extends AnyVal with DurationConversions
  6. implicit final class DurationInt extends AnyVal with DurationConversions
  7. implicit final class DurationLong extends AnyVal with DurationConversions
  8. final class FiniteDuration extends Duration

    This class represents a finite duration.

    This class represents a finite duration. Its addition and subtraction operators are overloaded to retain this guarantee statically. The range of this class is limited to +-(2^63-1)ns, which is roughly 292 years.

  9. implicit final class IntMult extends AnyVal
  10. implicit final class LongMult extends AnyVal
  11. type TimeUnit = java.util.concurrent.TimeUnit

Value Members

  1. final val DAYS: java.util.concurrent.TimeUnit(DAYS)
  2. final val HOURS: java.util.concurrent.TimeUnit(HOURS)
  3. final val MICROSECONDS: java.util.concurrent.TimeUnit(MICROSECONDS)
  4. final val MILLISECONDS: java.util.concurrent.TimeUnit(MILLISECONDS)
  5. final val MINUTES: java.util.concurrent.TimeUnit(MINUTES)
  6. final val NANOSECONDS: java.util.concurrent.TimeUnit(NANOSECONDS)
  7. final val SECONDS: java.util.concurrent.TimeUnit(SECONDS)
  8. implicit def durationToPair(d: Duration): (Long, TimeUnit)
  9. implicit def pairIntToDuration(p: (Int, TimeUnit)): Duration
  10. implicit def pairLongToDuration(p: (Long, TimeUnit)): FiniteDuration
  11. object Deadline extends java.io.Serializable
  12. object Duration extends java.io.Serializable
  13. object DurationConversions

    This object just holds some cogs which make the DSL machine work, not for direct consumption.

  14. object FiniteDuration extends java.io.Serializable
  15. object fromNow

    This object can be used as closing token for declaring a deadline at some future point in time:

    This object can be used as closing token for declaring a deadline at some future point in time:

    import scala.concurrent.duration._
    
    val deadline = 3 seconds fromNow
  16. object span

    This object can be used as closing token if you prefer dot-less style but do not want to enable language.postfixOps:

    This object can be used as closing token if you prefer dot-less style but do not want to enable language.postfixOps:

    import scala.concurrent.duration._
    
    val duration = 2 seconds span

Inherited from AnyRef

Inherited from Any

Ungrouped