Future

gears.async.Future
See theFuture companion object
trait Future[+T] extends OriginalSource[Try[T]], Cancellable

Futures are Sources that has the following properties:

  • They represent a single value: Once resolved, await-ing on a Future should always return the same value.
  • They can potentially be cancelled, via the cancel method.

There are two kinds of futures, active and passive.

  • '''Active''' futures are ones that are spawned with Future.apply and Task.start. They require the Async.Spawn context, and run on their own (as long as the Async.Spawn scope has not ended). Active futures represent concurrent computations within Gear's structured concurrency tree. Idiomatic Gears code should ''never'' return active futures. Should a function be async (i.e. takes an Async context parameter), they should return values or throw exceptions directly.
  • '''Passive''' futures are ones that are created by Future.Promise (through asFuture) and Future.withResolver. They represent yet-arrived values coming from ''outside'' of Gear's structured concurrency tree (for example, from network or the file system, or even from another concurrency system like Scala standard library futures). Idiomatic Gears libraries should return this kind of Future if deemed neccessary, but functions returning passive futures should ''not'' take an Async context.

Attributes

See also

Future.apply and Task.start for creating active futures.

Future.Promise and Future.withResolver for creating passive futures.

Future.awaitAll, Future.awaitFirst and Future.Collector for tools to work with multiple futures.

ScalaConverters.asGears and ScalaConverters.asScala for converting between Scala futures and Gears futures.

Companion
object
Graph
Supertypes
trait Cancellable
class OriginalSource[Try[T]]
trait Source[Try[T]]
class Object
trait Matchable
class Any
Show all
Known subtypes
trait Promise[T]

Members list

Value members

Concrete methods

def or(f2: Future[T]): Future[T]
Extension method from Future

Alternative parallel composition of this task with other task. If either task succeeds, succeed with the success that was returned first. Otherwise, fail with the failure that was returned last.

Alternative parallel composition of this task with other task. If either task succeeds, succeed with the success that was returned first. Otherwise, fail with the failure that was returned last.

Attributes

See also

orWithCancel for an alternative version where the slower future is cancelled.

inline def orImpl(inline withCancel: Boolean)(f2: Future[T]): Future[T]
Extension method from Future
def orWithCancel(f2: Future[T]): Future[T]
Extension method from Future

Like or but the slower future is cancelled. If either task succeeds, succeed with the success that was returned first and the other is cancelled. Otherwise, fail with the failure that was returned last.

Like or but the slower future is cancelled. If either task succeeds, succeed with the success that was returned first and the other is cancelled. Otherwise, fail with the failure that was returned last.

Attributes

def zip[U](f2: Future[U]): Future[(T, U)]
Extension method from Future

Parallel composition of two futures. If both futures succeed, succeed with their values in a pair. Otherwise, fail with the failure that was returned first.

Parallel composition of two futures. If both futures succeed, succeed with their values in a pair. Otherwise, fail with the failure that was returned first.

Attributes

Inherited methods

protected def addListener(k: Listener[Try[T]]): Unit

Add k to the listener set of this source.

Add k to the listener set of this source.

Attributes

Inherited from:
OriginalSource
final def awaitResult(using ac: Async): Try[T]

Waits for an item to arrive from the source. Suspends until an item returns.

Waits for an item to arrive from the source. Suspends until an item returns.

This is an utility method for direct waiting with Async, instead of going through listeners.

Attributes

Inherited from:
Source
def cancel(): Unit

Issue a cancel request

Issue a cancel request

Attributes

Inherited from:
Cancellable
def dropListener(k: Listener[Try[T]]): Unit

Signal that listener k is dead (i.e. will always fail to acquire locks from now on), and should be removed from onComplete queues.

Signal that listener k is dead (i.e. will always fail to acquire locks from now on), and should be removed from onComplete queues.

This permits original, (i.e. non-derived) sources like futures or channels to drop the listener from their waiting sets.

Attributes

Inherited from:
Source
def onComplete(k: Listener[Try[T]]): Unit

Once data is available, pass it to the listener k. onComplete is always non-blocking.

Once data is available, pass it to the listener k. onComplete is always non-blocking.

Note that k's methods will be executed on the same thread as the Source, usually in sequence. It is hence important that the listener itself does not perform expensive operations.

Attributes

Inherited from:
OriginalSource
def poll(): Option[Try[T]]

Similar to poll, but instead of passing in a listener, directly return the value T if it is available.

Similar to poll, but instead of passing in a listener, directly return the value T if it is available.

Attributes

Inherited from:
Source
def poll(k: Listener[Try[T]]): Boolean

Checks whether data is available at present and pass it to k if so. Calls to poll are always synchronous and non-blocking.

Checks whether data is available at present and pass it to k if so. Calls to poll are always synchronous and non-blocking.

The process is as follows:

  • If no data is immediately available, return false immediately.
  • If there is data available, attempt to lock k.
    • If k is no longer available, true is returned to signal this source's general availability.
    • If locking k succeeds:
      • If data is still available, complete k and return true.
      • Otherwise, unlock k and return false.

Note that in all cases, a return value of false indicates that k should be put into onComplete to receive data in a later point in time.

Attributes

Returns

Whether poll was able to pass data to k. Note that this is regardless of k being available to receive the data. In most cases, one should pass k into Source!.onComplete if poll returns false.

Inherited from:
Source