Overview
Values that are computed one time, when they are first forced. Safe for threads.
(import (std lazy))
(def table (delay (load-table "big.dat"))) ; nothing is loaded yet
(force table) ; loads the table
(force table) ; the same table, not loaded again
One run. Exactly one thread runs the thunk at a time. A thread that forces the value while another thread runs the thunk waits for it. The wait blocks the thread, not only the fiber.
Errors. If the thunk raises, the exception goes to the force that ran the thunk. The value is not kept as an error: the next force runs the thunk again. A waiting thread also runs it again. Thus a cancelled or failed force does not make the value fail for all callers.
A thunk that forces itself. A force of the value from its own
thunk raises InvalidOperationException. A wait there can not
complete.
Dynamic environment. The thunk runs in the dynamic environment
of the force that runs it: its ports, parameters and cancellation. To
make the value the same for all callers, set the parameters in the
thunk with parameterize.
Not async. The thunk can not suspend. A body that calls a bjoroutine is a type error.
See also: delay, force, lazy-ready, lazy-forced?
Reference
Types
Lazy
type alias ↑ Types
A value of type %a that is computed when it is first forced.
%a- The type of the value.
FiberLazy
record ↑ Types
A value of type %a that an async thunk computes when it is first forced.
%a- The type of the value.
The same rules as Lazy for one run and for errors. The
differences:
A wait suspends. A fiber that forces the value while another fiber runs the thunk suspends. It does not block its thread.
Cancellation. The wait is a sync. When the scope of the
waiting fiber is cancelled, force/fiber raises, as each
sync does. If the scope of the running fiber is cancelled, its
run fails, and a waiting fiber runs the thunk again.
A thunk that forces itself. The force waits for its own run, and does not complete. It is not detected.
See also: delay/fiber, force/fiber, lazy-evt
Functions
make-lazy
function ↑ Functions
(: make-lazy (-> (-> %a) (Lazy %a)))
A lazy value that a thunk computes when it is first forced.
thunk- A function of no arguments. It is not async.
- returns
- A lazy value that is not forced.
lazy-ready
function ↑ Functions
(: lazy-ready (-> %a (Lazy %a)))
A lazy value that is already forced.
value- The value.
- returns
- A lazy value. A force of it returns value.
(: (Lazy int))
(def answer (if cached? (lazy-ready 42) (delay (compute))))
force
function ↑ Functions
(: force (-> (Lazy %a) %a))
The value of a lazy value. Runs its thunk if no force has completed.
lazy- The lazy value.
- returns
- The value. All forces that complete return the same value.
- raises
System.InvalidOperationException - When the thunk of lazy forces lazy.
When the value is forced, a force is one read of a field. If the thunk raises, force raises the same exception, and the value stays not forced.
See also: delay, lazy-forced?
lazy-forced?
function ↑ Functions
(: lazy-forced? (-> (Lazy %a) bool))
Whether a lazy value has a value.
lazy- The lazy value.
- returns
- #t when a force has completed. Does not run the thunk.
See also: force
make-fiber-lazy
function ↑ Functions
(: make-fiber-lazy (-> (-bjo-> %a) (FiberLazy %a)))
(make-fiber-lazy thunk)
A lazy value that an async thunk computes when it is first forced.
thunk- A function of no arguments. It can suspend.
- returns
- A lazy value that is not forced.
See also: delay/fiber, force/fiber
force/fiber
function ↑ Functions
(: force/fiber (-bjo-> (FiberLazy %a) %a))
(force/fiber lazy)
The value of an async lazy value. Runs its thunk if no force has completed.
lazy- The lazy value.
- returns
- The value. All forces that complete return the same value.
- raises
Bjolang.Runtime.Cancelled - When the scope is cancelled while the force waits for another fiber.
A yield point. When the value is forced, it returns without a suspension. If the thunk raises, force/fiber raises the same exception, and the value stays not forced.
See also: delay/fiber, fiber-lazy-forced?, lazy-evt
fiber-lazy-forced?
function ↑ Functions
(: fiber-lazy-forced? (-> (FiberLazy %a) bool))
(fiber-lazy-forced? lazy)
Whether an async lazy value has a value.
lazy- The lazy value.
- returns
- #t when a force has completed. Does not run the thunk.
See also: force/fiber
lazy-evt
function ↑ Functions
(: lazy-evt (-> (FiberLazy %a) (Event %a)))
(lazy-evt lazy)
The event of an async lazy value getting its value.
lazy- The lazy value.
- returns
- An event whose value is the value of lazy.
(choose (lazy-evt config) (timeout-evt (seconds 5)))
The event does not run the thunk. It becomes ready when a force by another fiber completes. A run that fails does not make it ready.
See also: force/fiber
Macros
delay
macro ↑ Macros
(delay body ...)
A lazy value that body computes when it is first forced.
body ...- The expressions to run at the first force. The last one gives the value.
(def p (delay (println "computing") 42))
(force p) ; prints "computing", returns 42
(force p) ; returns 42
The same as (make-lazy (fun () body ...)).
delay/fiber
macro ↑ Macros
(delay/fiber body ...)
A lazy value that an async body computes when it is first forced.
body ...- The expressions to run at the first force. They can suspend. The last one gives the value.
(def config (delay/fiber (parse-config (fetch config-url))))
(force/fiber config) ; fetches one time; other fibers wait for it
The same as (make-fiber-lazy (bjoroutine () body ...)).
See also: force/fiber, make-fiber-lazy, delay