delay

de.rmgk.delay$
object delay

Work with descriptions of computations that you want to execute later. Delay offers two abstractions Sync and Async.

Sync which behaves like a function () => A its only purpose is to enable much more efficient composition by using macros to transform any delay.map and delay.flatMap calls into just sequential code if everything is inlined, while falling back to passing around function pointers if you pass the Sync as parameters. Sync is particularly useful for composition of code with side effects.

Async similarly allows composition of asynchronous computations. It behaves similarly to a (A => Unit) => Unit (i.e., a function that takes a callback). The purpose of Async is not only efficiency, but also to provide an alternative syntax to for expressions. Specifically, async blocks allow sequential looking composition of async code:

Async {
 val x = async1.bind
 async2.bind
 println("output")
 val y = async3.bind
 x + y
}

Which is roughly equivalent to:

for {
 x <- async1
 _ <- async2
 y <- {
   println("output")
   async3
} yield { x + y }

Thus just a bit more regular than for expressions. Note that you may not use bind in any nested expressions, only as the outer method in a single statement, or as the right hand side of a val binding.

Note that Sync and in particular Async are just abstractions, there is no additional runtime or internal state beyond composition of functions. In particular, Async has no concept of parallel excution or threads or anything like that. Using delay.run (and thus at some point Async.bind) simply executes whatever the code you have written in the async handlers and likely the initial AsyncCompanion.fromCallback.

If you need to execute anything on a different thread you can easily do so by using whatever library for threading you fancy. We provide convenience functions to convert to and from scala.concurrent.Future:

Async {
 async1.runToFuture.map(x => x + 1).toAsync.bind
}

Attributes

Graph
Supertypes
class Object
trait Matchable
class Any
Self type
delay.type

Members list

Concise view

Type members

Classlikes

class Async[-Ctx, +A](val handleInCtx: Ctx => Callback[A] => Unit)

A description of a computation that returns an A at some later point in time. This could be seen as a (A => Unit) => Unit but with error handling and an additional context to store some information threaded through the asynchronous execution. You probably do not want to execute this directly

A description of a computation that returns an A at some later point in time. This could be seen as a (A => Unit) => Unit but with error handling and an additional context to store some information threaded through the asynchronous execution. You probably do not want to execute this directly

Attributes

Graph
Supertypes
class Object
trait Matchable
class Any
Known subtypes
class Sync[Ctx, A]
trait AsyncCompanion[Ctx]

Companion object of Async, but with a type parameter to allow fine grained type inference. This could potentially be simplified if we ever get named type parameters.

Companion object of Async, but with a type parameter to allow fine grained type inference. This could potentially be simplified if we ever get named type parameters.

Attributes

Graph
Supertypes
class Object
trait Matchable
class Any
@FunctionalInterface
trait Callback[-A]

A callback that also handles failure.

A callback that also handles failure.

Attributes

Graph
Supertypes
class Object
trait Matchable
class Any
Known subtypes
class Promise[T]

Attributes

Graph
Supertypes
class Object
trait Matchable
class Any
Self type
class Promise[T] extends Callback[T]

Simple promise implementation synchronizing on the object monitor.

Simple promise implementation synchronizing on the object monitor.

Attributes

Graph
Supertypes
trait Callback[T]
class Object
trait Matchable
class Any
class Sync[-Ctx, +A](val runInContext: Ctx => A) extends Async[Ctx, A]

Description of a computation that returns an A immediately without blocking. This could be seen as a () => A but with an additional Ctx to store context information.

Description of a computation that returns an A immediately without blocking. This could be seen as a () => A but with an additional Ctx to store context information.

Attributes

Graph
Supertypes
class Async[Ctx, A]
class Object
trait Matchable
class Any
class SyncCompanion[Ctx]

Companion object of Sync, but with a type parameter to allow fine grained type inference. This could potentially be simplified if we ever get named type parameters.

Companion object of Sync, but with a type parameter to allow fine grained type inference. This could potentially be simplified if we ever get named type parameters.

Attributes

Graph
Supertypes
class Object
trait Matchable
class Any

Value members

Concrete methods

inline def Async[Ctx]: AsyncCompanion[Ctx]

Syntactic convenience to enable type inference of Ctx.

Syntactic convenience to enable type inference of Ctx.

Attributes

inline def Sync[Ctx]: SyncCompanion[Ctx]

Extensions

Extensions

extension [Ctx, A](async: Async[Ctx, A])
inline def after(inline body: Ctx ?=> Either[Throwable, A] => Unit): Async[Ctx, A]

Runs body after the asynchronous computation is finished. Useful for running cleanup handlers.

Runs body after the asynchronous computation is finished. Useful for running cleanup handlers.

Attributes

inline def flatMap[B](inline f: A => Async[Ctx, B]): Async[Ctx, B]
inline def map[B](inline f: A => B): Async[Ctx, B]
inline def run(inline cb: Callback[A])(using inline ctx: Ctx): Unit

Start the underlying computation and pass the result to cb.

Start the underlying computation and pass the result to cb.

Attributes

inline def runToAsync(using inline ctx: Ctx): Async[Ctx, A]

Start the underlying computation immediately. The result is cached and can be accessed as Async

Start the underlying computation immediately. The result is cached and can be accessed as Async

Attributes

inline def runToFuture(using inline ctx: Ctx): Future[A]

Start the underlying computation immediately. Return a Future of the result.

Start the underlying computation immediately. Return a Future of the result.

Attributes

extension [Ctx, T](cs: CompletionStage[T])
inline def toAsync: Async[Ctx, T]
extension [A](fut: Future[A])
inline def toAsync(using inline ec: ExecutionContext): Async[Any, A]
extension [Ctx, A](sync: Sync[Ctx, A])
inline def flatMap[B](inline f: A => Sync[Ctx, B]): Sync[Ctx, B]
inline def map[B](inline f: A => B): Sync[Ctx, B]
inline def run(using inline ctx: Ctx): A

Executes the Sync given a Ctx.

Executes the Sync given a Ctx.

Attributes