de.rmgk
Members list
Type members
Classlikes
Work with descriptions of computations that you want to execute later. Delay offers two abstractions Sync and Async.
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 Objecttrait Matchableclass Any
- Self type
- delay.type