|
| 1 | +/*** |
| 2 | + This module provides a type `Lazy.t` and functions to create and |
| 3 | + manipulate lazy values. A lazy value is a value that is not |
| 4 | + computed until it is needed. This is useful for deferring |
| 5 | + computations that may be expensive or unnecessary. |
| 6 | + */ |
| 7 | + |
| 8 | +/** |
| 9 | + The type of a lazy value. `Lazy.t<'a>` represents a lazy value |
| 10 | + that will eventually yield a value of type `'a` when accessed. |
| 11 | + The value is computed only once, and the result is cached for |
| 12 | + subsequent accesses. If the computation raises an exception, |
| 13 | + the same exception is raised again on subsequent accesses. |
| 14 | + */ |
| 15 | +type t<'a> = lazy_t<'a> |
| 16 | + |
| 17 | +/** |
| 18 | + `Lazy.make(f)` creates a lazy value from `f` which is the |
| 19 | + computation to be deferred of type `unit => 'a`. |
| 20 | + The function returns a lazy value of type `Lazy.t<'a>`. |
| 21 | + The computation is not executed until the lazy value is accessed. |
| 22 | + |
| 23 | + ## Examples |
| 24 | + ```rescript |
| 25 | + let lazyValue = Lazy.make(() => { |
| 26 | + // Some expensive computation |
| 27 | + Console.log("Computing...") |
| 28 | + 42 |
| 29 | + }); |
| 30 | + lazyValue->Lazy.get->assertEqual(42) |
| 31 | + ``` |
| 32 | +*/ |
| 33 | +let make: (unit => 'a) => t<'a> |
| 34 | + |
| 35 | +/** |
| 36 | + `Lazy.get(x)` forces the suspension `x` and returns its result. |
| 37 | + If `x` has already been forced, `Lazy.get(x)` returns the |
| 38 | + same value again without recomputing it. If it raised an |
| 39 | + exception, the same exception is raised again. |
| 40 | + Raise `Undefined` if the forcing of `x` tries to force `x` itself |
| 41 | + recursively. This is a runtime error. |
| 42 | + */ |
| 43 | +let get: t<'a> => 'a |
| 44 | + |
| 45 | +/** |
| 46 | + `Lazy.isEvaluated(x)` returns `true` if the suspension `x` has |
| 47 | + already been forced and did not raise an exception. Otherwise, |
| 48 | + it returns `false`. This is useful for checking if a lazy value |
| 49 | + has been computed before accessing it. |
| 50 | + |
| 51 | + ## Examples |
| 52 | + ```rescript |
| 53 | + let lazyValue = Lazy.make(() => { |
| 54 | + // Some expensive computation |
| 55 | + Console.log("Computing...") |
| 56 | + 42 |
| 57 | + }) |
| 58 | + Lazy.isEvaluated(lazyValue)->assertEqual(false) |
| 59 | + lazyValue->Lazy.get->assertEqual(42) |
| 60 | + lazyValue->Lazy.isEvaluated->assertEqual(true) |
| 61 | + ``` |
| 62 | + */ |
| 63 | +let isEvaluated: t<'a> => bool |
| 64 | + |
| 65 | +exception Undefined |
| 66 | + |
| 67 | +/** |
| 68 | + `force(x)` forces the suspension `x` and returns its result. |
| 69 | + If `x` has already been forced, `Lazy.force(x)` returns the |
| 70 | + same value again without recomputing it. If it raised an exception, |
| 71 | + the same exception is raised again. |
| 72 | + Raise `Undefined` if the forcing of `x` tries to force `x` itself |
| 73 | + recursively. |
| 74 | +*/ |
| 75 | +@deprecated("Use `Lazy.get` instead") |
| 76 | +let force: t<'a> => 'a |
| 77 | + |
| 78 | +/** |
| 79 | + `force_val(x)` forces the suspension `x` and returns its |
| 80 | + result. If `x` has already been forced, `force_val(x)` |
| 81 | + returns the same value again without recomputing it. |
| 82 | + Raise `Undefined` if the forcing of `x` tries to force `x` itself |
| 83 | + recursively. |
| 84 | + If the computation of `x` raises an exception, it is unspecified |
| 85 | + whether `force_val(x)` raises the same exception or `Undefined`. |
| 86 | +*/ |
| 87 | +@deprecated("Use `Lazy.get` instead") |
| 88 | +let force_val: t<'a> => 'a |
| 89 | + |
| 90 | +/** |
| 91 | + `Lazy.from_fun(f)` creates a lazy value from `f` which is the |
| 92 | + computation to be deferred of type `unit => 'a`. |
| 93 | + The function returns a lazy value of type `Lazy.t<'a>`. |
| 94 | + The computation is not executed until the lazy value is accessed. |
| 95 | +*/ |
| 96 | +@deprecated("Use `Lazy.make` instead") |
| 97 | +let from_fun: (unit => 'a) => t<'a> |
| 98 | + |
| 99 | +/** |
| 100 | + `from_val(v)` returns an already-forced suspension of `v`. |
| 101 | + This is for special purposes only. |
| 102 | +*/ |
| 103 | +@deprecated("Use `Lazy.make` instead") |
| 104 | +let from_val: 'a => t<'a> |
| 105 | + |
| 106 | +/** |
| 107 | + `is_val(x)` returns `true` if `x has already been forced and |
| 108 | + did not raise an exception. |
| 109 | +*/ |
| 110 | +@deprecated("Use `Lazy.isEvaluated` instead") |
| 111 | +let is_val: t<'a> => bool |
0 commit comments