Skip to content

Commit 0ff2966

Browse files
committed
move Lazy module to Stdlib
1 parent 72d534a commit 0ff2966

21 files changed

+237
-139
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737

3838
- Improve error message for missing value when the identifier is also the name of a module in scope. https://github.com/rescript-lang/rescript/pull/7384
3939
- Upgrade Flow parser to 0.267.0. https://github.com/rescript-lang/rescript/pull/7390
40+
- Move `Lazy` module to Stdlib. https://github.com/rescript-lang/rescript/pull/7399
4041

4142
# 12.0.0-alpha.11
4243

lib/es6/Stdlib.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ function assertEqual(a, b) {
1212
RE_EXN_ID: "Assert_failure",
1313
_1: [
1414
"Stdlib.res",
15-
119,
15+
120,
1616
4
1717
],
1818
Error: new Error()
@@ -49,6 +49,8 @@ let $$Intl;
4949

5050
let $$JSON;
5151

52+
let Lazy;
53+
5254
let List;
5355

5456
let $$Math;
@@ -133,6 +135,7 @@ export {
133135
Int,
134136
$$Intl,
135137
$$JSON,
138+
Lazy,
136139
List,
137140
$$Math,
138141
Null,

lib/es6/Lazy.js renamed to lib/es6/Stdlib_Lazy.js

+9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
import * as Primitive_lazy from "./Primitive_lazy.js";
44

5+
let make = Primitive_lazy.from_fun;
6+
7+
let get = Primitive_lazy.force;
8+
9+
let isEvaluated = Primitive_lazy.is_val;
10+
511
let Undefined = Primitive_lazy.Undefined;
612

713
let force = Primitive_lazy.force;
@@ -15,6 +21,9 @@ let from_val = Primitive_lazy.from_val;
1521
let is_val = Primitive_lazy.is_val;
1622

1723
export {
24+
make,
25+
get,
26+
isEvaluated,
1827
Undefined,
1928
force,
2029
force_val,

lib/js/Stdlib.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ function assertEqual(a, b) {
1212
RE_EXN_ID: "Assert_failure",
1313
_1: [
1414
"Stdlib.res",
15-
119,
15+
120,
1616
4
1717
],
1818
Error: new Error()
@@ -49,6 +49,8 @@ let $$Intl;
4949

5050
let $$JSON;
5151

52+
let Lazy;
53+
5254
let List;
5355

5456
let $$Math;
@@ -132,6 +134,7 @@ exports.Float = Float;
132134
exports.Int = Int;
133135
exports.$$Intl = $$Intl;
134136
exports.$$JSON = $$JSON;
137+
exports.Lazy = Lazy;
135138
exports.List = List;
136139
exports.$$Math = $$Math;
137140
exports.Null = Null;

lib/js/Lazy.js renamed to lib/js/Stdlib_Lazy.js

+9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
let Primitive_lazy = require("./Primitive_lazy.js");
44

5+
let make = Primitive_lazy.from_fun;
6+
7+
let get = Primitive_lazy.force;
8+
9+
let isEvaluated = Primitive_lazy.is_val;
10+
511
let Undefined = Primitive_lazy.Undefined;
612

713
let force = Primitive_lazy.force;
@@ -14,6 +20,9 @@ let from_val = Primitive_lazy.from_val;
1420

1521
let is_val = Primitive_lazy.is_val;
1622

23+
exports.make = make;
24+
exports.get = get;
25+
exports.isEvaluated = isEvaluated;
1726
exports.Undefined = Undefined;
1827
exports.force = force;
1928
exports.force_val = force_val;

packages/artifacts.txt

+8-8
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,6 @@ lib/es6/Jsx.js
114114
lib/es6/JsxDOM.js
115115
lib/es6/JsxDOMStyle.js
116116
lib/es6/JsxEvent.js
117-
lib/es6/Lazy.js
118117
lib/es6/Obj.js
119118
lib/es6/Pervasives.js
120119
lib/es6/Primitive_array.js
@@ -178,6 +177,7 @@ lib/es6/Stdlib_Intl_Segmenter.js
178177
lib/es6/Stdlib_Intl_Segments.js
179178
lib/es6/Stdlib_Iterator.js
180179
lib/es6/Stdlib_JSON.js
180+
lib/es6/Stdlib_Lazy.js
181181
lib/es6/Stdlib_List.js
182182
lib/es6/Stdlib_Map.js
183183
lib/es6/Stdlib_Math.js
@@ -287,7 +287,6 @@ lib/js/Jsx.js
287287
lib/js/JsxDOM.js
288288
lib/js/JsxDOMStyle.js
289289
lib/js/JsxEvent.js
290-
lib/js/Lazy.js
291290
lib/js/Obj.js
292291
lib/js/Pervasives.js
293292
lib/js/Primitive_array.js
@@ -351,6 +350,7 @@ lib/js/Stdlib_Intl_Segmenter.js
351350
lib/js/Stdlib_Intl_Segments.js
352351
lib/js/Stdlib_Iterator.js
353352
lib/js/Stdlib_JSON.js
353+
lib/js/Stdlib_Lazy.js
354354
lib/js/Stdlib_List.js
355355
lib/js/Stdlib_Map.js
356356
lib/js/Stdlib_Math.js
@@ -809,12 +809,6 @@ lib/ocaml/JsxEvent.cmi
809809
lib/ocaml/JsxEvent.cmj
810810
lib/ocaml/JsxEvent.cmt
811811
lib/ocaml/JsxEvent.res
812-
lib/ocaml/Lazy.cmi
813-
lib/ocaml/Lazy.cmj
814-
lib/ocaml/Lazy.cmt
815-
lib/ocaml/Lazy.cmti
816-
lib/ocaml/Lazy.res
817-
lib/ocaml/Lazy.resi
818812
lib/ocaml/Obj.cmi
819813
lib/ocaml/Obj.cmj
820814
lib/ocaml/Obj.cmt
@@ -1109,6 +1103,12 @@ lib/ocaml/Stdlib_JSON.cmt
11091103
lib/ocaml/Stdlib_JSON.cmti
11101104
lib/ocaml/Stdlib_JSON.res
11111105
lib/ocaml/Stdlib_JSON.resi
1106+
lib/ocaml/Stdlib_Lazy.cmi
1107+
lib/ocaml/Stdlib_Lazy.cmj
1108+
lib/ocaml/Stdlib_Lazy.cmt
1109+
lib/ocaml/Stdlib_Lazy.cmti
1110+
lib/ocaml/Stdlib_Lazy.res
1111+
lib/ocaml/Stdlib_Lazy.resi
11121112
lib/ocaml/Stdlib_List.cmi
11131113
lib/ocaml/Stdlib_List.cmj
11141114
lib/ocaml/Stdlib_List.cmt

runtime/Lazy.resi

-41
This file was deleted.

runtime/Stdlib.res

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ module Float = Stdlib_Float
1313
module Int = Stdlib_Int
1414
module Intl = Stdlib_Intl
1515
module JSON = Stdlib_JSON
16+
module Lazy = Stdlib_Lazy
1617
module List = Stdlib_List
1718
module Math = Stdlib_Math
1819
module Null = Stdlib_Null

runtime/Lazy.res renamed to runtime/Stdlib_Lazy.res

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
// FIXME:
2-
// This exists for compatibility reason.
3-
// Move this into Pervasives or Core
4-
51
type t<'a> = lazy_t<'a>
62

73
exception Undefined = Primitive_lazy.Undefined
84

5+
let make = Primitive_lazy.from_fun
6+
7+
let get = Primitive_lazy.force
8+
9+
let isEvaluated = Primitive_lazy.is_val
10+
911
let force = Primitive_lazy.force
1012

1113
let force_val = Primitive_lazy.force_val

runtime/Stdlib_Lazy.resi

+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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

tests/tests/src/gpr_3697_test.mjs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
// Generated by ReScript, PLEASE EDIT WITH CARE
22

3-
import * as Lazy from "rescript/lib/es6/Lazy.js";
3+
import * as Stdlib_Lazy from "rescript/lib/es6/Stdlib_Lazy.js";
44

55
function fix() {
66
return {
77
TAG: "Fix",
8-
_0: Lazy.from_fun(fix)
8+
_0: Stdlib_Lazy.from_fun(fix)
99
};
1010
}
1111

1212
function unfixLeak(_f) {
1313
while (true) {
1414
let f = _f;
15-
_f = Lazy.force(f._0);
15+
_f = Stdlib_Lazy.force(f._0);
1616
continue;
1717
};
1818
}
1919

2020
function unfix(p) {
2121
while (true) {
2222
let h = p.contents;
23-
p.contents = Lazy.force(h._0);
23+
p.contents = Stdlib_Lazy.force(h._0);
2424
};
2525
}
2626

0 commit comments

Comments
 (0)