Skip to content

Commit 42c4a66

Browse files
authored
update documentation about lazy values (#1002)
1 parent 4214b1c commit 42c4a66

File tree

1 file changed

+21
-54
lines changed

1 file changed

+21
-54
lines changed

pages/docs/manual/v12.0.0/lazy-values.mdx

+21-54
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ canonical: "/docs/manual/v12.0.0/lazy-values"
66

77
# Lazy Value
88

9-
If you have some expensive computations you'd like to **defer and cache** subsequently, you can wrap it with `lazy`:
9+
If you have some expensive computations you'd like to **defer and cache** subsequently, you can turn them into *lazy* values:
1010

1111
<CodeTab labels={["ReScript", "JS Output"]}>
1212

@@ -15,83 +15,50 @@ If you have some expensive computations you'd like to **defer and cache** subseq
1515
external readdirSync: string => array<string> = "readdirSync"
1616
1717
// Read the directory, only once
18-
let expensiveFilesRead = lazy({
18+
let expensiveFilesRead = Lazy.make(() => {
1919
Console.log("Reading dir")
2020
readdirSync("./pages")
2121
})
2222
```
2323
```js
24-
var Fs = require("fs");
25-
26-
var expensiveFilesRead = {
27-
LAZY_DONE: false,
28-
VAL: (function () {
29-
console.log("Reading dir");
30-
return Fs.readdirSync("./pages");
31-
})
32-
};
24+
import * as Lazy from "./stdlib/Lazy.js";
25+
import * as Nodefs from "node:fs";
26+
27+
let expensiveFilesRead = Lazy.make(() => {
28+
console.log("Reading dir");
29+
return Nodefs.readdirSync("./pages");
30+
});
31+
3332
```
3433

3534
</CodeTab>
3635

37-
Check the JS Output tab: that `expensiveFilesRead`'s code isn't executed yet, even though you declared it! You can carry it around without fearing that it'll run the directory read.
38-
3936
**Note**: a lazy value is **not** a [shared data type](shared-data-types.md). Don't rely on its runtime representation in your JavaScript code.
4037

4138
## Execute The Lazy Computation
4239

43-
To actually run the lazy value's computation, use `Lazy.force` from the globally available `Lazy` module:
40+
To actually run the lazy value's computation, use `Lazy.get` from the standard library `Lazy` module:
4441

4542
<CodeTab labels={["ReScript", "JS Output"]}>
4643

4744
```res example
4845
// First call. The computation happens
49-
Console.log(Lazy.force(expensiveFilesRead)) // logs "Reading dir" and the directory content
46+
Console.log(Lazy.get(expensiveFilesRead)) // logs "Reading dir" and the directory content
5047
5148
// Second call. Will just return the already calculated result
52-
Console.log(Lazy.force(expensiveFilesRead)) // logs the directory content
49+
Console.log(Lazy.get(expensiveFilesRead)) // logs the directory content
5350
```
5451
```js
55-
console.log(CamlinternalLazy.force(expensiveFilesRead));
56-
57-
console.log(CamlinternalLazy.force(expensiveFilesRead));
58-
```
59-
60-
</CodeTab>
61-
62-
The first time `Lazy.force` is called, the expensive computation happens and the result is **cached**. The second time, the cached value is directly used.
63-
64-
**You can't re-trigger the computation after the first `force` call**. Make sure you only use a lazy value with computations whose results don't change (e.g. an expensive server request whose response is always the same).
65-
66-
Instead of using `Lazy.force`, you can also use [pattern matching](pattern-matching-destructuring.md) to trigger the computation:
67-
68-
<CodeTab labels={["ReScript", "JS Output"]}>
52+
console.log(Lazy.get(expensiveFilesRead));
6953

70-
```res example
71-
switch expensiveFilesRead {
72-
| lazy(result) => Console.log(result)
73-
}
74-
```
75-
```js
76-
var result = CamlinternalLazy.force(expensiveFilesRead);
54+
console.log(Lazy.get(expensiveFilesRead));
7755
```
7856

7957
</CodeTab>
8058

81-
Since pattern matching also works on a `let` binding, you can also do:
82-
83-
<CodeTab labels={["ReScript", "JS Output"]}>
84-
85-
```res example
86-
let lazy(result) = expensiveFilesRead
87-
Console.log(result)
88-
```
89-
```js
90-
var result = CamlinternalLazy.force(expensiveFilesRead);
91-
console.log(result);
92-
```
59+
The first time `Lazy.get` is called, the expensive computation happens and the result is **cached**. The second time, the cached value is directly used.
9360

94-
</CodeTab>
61+
**You can't re-trigger the computation after the first `get` call**. Make sure you only use a lazy value with computations whose results don't change (e.g. an expensive server request whose response is always the same).
9562

9663
## Exception Handling
9764

@@ -101,18 +68,18 @@ For completeness' sake, our files read example might raise an exception because
10168

10269
```res example
10370
let result = try {
104-
Lazy.force(expensiveFilesRead)
71+
Lazy.get(expensiveFilesRead)
10572
} catch {
10673
| Not_found => [] // empty array of files
10774
}
10875
```
10976
```js
110-
var result;
77+
let result;
11178

11279
try {
113-
result = CamlinternalLazy.force(expensiveFilesRead);
80+
result = Lazy.get(expensiveFilesRead);
11481
} catch (raw_exn) {
115-
var exn = Caml_js_exceptions.internalToOCamlException(raw_exn);
82+
let exn = Primitive_exceptions.internalToException(raw_exn);
11683
if (exn.RE_EXN_ID === "Not_found") {
11784
result = [];
11885
} else {

0 commit comments

Comments
 (0)