Package kotlin.coroutines.experimental

Platform and version requirements: Kotlin 1.1

Library support for coroutines, including support for lazy sequences.

Types

AbstractCoroutineContextElement

abstract class AbstractCoroutineContextElement : Element

Base class for CoroutineContext.Element implementations.

Continuation

interface Continuation<in T>

Interface representing a continuation after a suspension point that returns value of type T.

ContinuationInterceptor

interface ContinuationInterceptor : Element

Marks coroutine context element that intercepts coroutine continuations. The coroutines framework uses ContinuationInterceptor.Key to retrieve the interceptor and intercepts all coroutine continuations with interceptContinuation invocations.

CoroutineContext

interface CoroutineContext

Persistent context for the coroutine. It is an indexed set of Element instances. An indexed set is a mix between a set and a map. Every element in this set has a unique Key. Keys are compared by reference.

EmptyCoroutineContext

object EmptyCoroutineContext : CoroutineContext

An empty coroutine context.

SequenceBuilder

abstract class SequenceBuilder<in T>

Builder for a Sequence or an Iterator, provides yield and yieldAll suspension functions.

Annotations

RestrictsSuspension

annotation class RestrictsSuspension

Classes and interfaces marked with this annotation are restricted when used as receivers for extension suspend functions. These suspend extensions can only invoke other member or extension suspend functions on this particular receiver only and are restricted from calling arbitrary suspension functions.

Functions

buildIterator

fun <T> buildIterator(
    builderAction: suspend SequenceBuilder<T>.() -> Unit
): Iterator<T>

Builds an Iterator lazily yielding values one by one.

buildSequence

fun <T> buildSequence(
    builderAction: suspend SequenceBuilder<T>.() -> Unit
): Sequence<T>

Builds a Sequence lazily yielding values one by one.

createCoroutine

fun <R, T> (suspend R.() -> T).createCoroutine(
    receiver: R,
    completion: Continuation<T>
): Continuation<Unit>

Creates a coroutine with receiver type R and result type T. This function creates a new, fresh instance of suspendable computation every time it is invoked.

fun <T> (suspend () -> T).createCoroutine(
    completion: Continuation<T>
): Continuation<Unit>

Creates a coroutine without receiver and with result type T. This function creates a new, fresh instance of suspendable computation every time it is invoked.

startCoroutine

fun <R, T> (suspend R.() -> T).startCoroutine(
    receiver: R,
    completion: Continuation<T>)

Starts coroutine with receiver type R and result type T. This function creates and start a new, fresh instance of suspendable computation every time it is invoked. The completion continuation is invoked when coroutine completes with result or exception.

fun <T> (suspend () -> T).startCoroutine(
    completion: Continuation<T>)

Starts coroutine without receiver and with result type T. This function creates and start a new, fresh instance of suspendable computation every time it is invoked. The completion continuation is invoked when coroutine completes with result or exception.

suspendCoroutine

suspend fun <T> suspendCoroutine(
    block: (Continuation<T>) -> Unit
): T

Obtains the current continuation instance inside suspend functions and suspends currently running coroutine.