1Stdlib.Lazy(3)                   OCaml library                  Stdlib.Lazy(3)
2
3
4

NAME

6       Stdlib.Lazy - no description
7

Module

9       Module   Stdlib.Lazy
10

Documentation

12       Module Lazy
13        : (module Stdlib__Lazy)
14
15
16
17
18
19
20
21       type 'a t = 'a CamlinternalLazy.t
22
23
24       A  value  of type 'a Lazy.t is a deferred computation, called a suspen‐
25       sion, that has a result of type 'a .   The  special  expression  syntax
26       lazy  (expr)  makes  a  suspension of the computation of expr , without
27       computing expr itself yet.  "Forcing" the suspension will then  compute
28       expr and return its result. Matching a suspension with the special pat‐
29       tern syntax lazy(pattern) also computes the underlying  expression  and
30       tries to bind it to pattern :
31
32
33           let lazy_option_map f x =
34           match x with
35           | lazy (Some x) -> Some (Lazy.force f x)
36           | _ -> None
37
38
39       Note:  If lazy patterns appear in multiple cases in a pattern-matching,
40       lazy expressions may be forced even outside of the case ultimately  se‐
41       lected  by the pattern matching. In the example above, the suspension x
42       is always computed.
43
44       Note: lazy_t is the built-in type constructor used by the compiler  for
45       the  lazy  keyword.  You should not use it directly.  Always use Lazy.t
46       instead.
47
48       Note: Lazy.force is not thread-safe.  If  you  use  this  module  in  a
49       multi-threaded program, you will need to add some locks.
50
51       Note: if the program is compiled with the -rectypes option, ill-founded
52       recursive definitions of the form let rec x = lazy x or  let  rec  x  =
53       lazy(lazy(...(lazy x))) are accepted by the type-checker and lead, when
54       forced, to ill-formed values that trigger infinite loops in the garbage
55       collector  and  other  parts of the run-time system.  Without the -rec‐
56       types option, such ill-founded recursive definitions  are  rejected  by
57       the type-checker.
58
59
60
61       exception Undefined
62
63
64
65
66
67       val force : 'a t -> 'a
68
69
70       force  x  forces the suspension x and returns its result.  If x has al‐
71       ready been forced, Lazy.force x returns the same  value  again  without
72       recomputing  it.   If  it  raised  an  exception, the same exception is
73       raised again.
74
75
76       Raises Undefined if the forcing of x tries to  force  x  itself  recur‐
77       sively.
78
79
80
81
82   Iterators
83       val map : ('a -> 'b) -> 'a t -> 'b t
84
85
86       map  f x returns a suspension that, when forced, forces x and applies f
87       to its value.
88
89       It is equivalent to lazy (f (Lazy.force x)) .
90
91
92       Since 4.13.0
93
94
95
96
97   Reasoning on already-forced suspensions
98       val is_val : 'a t -> bool
99
100
101       is_val x returns true if x has already been forced and did not raise an
102       exception.
103
104
105       Since 4.00.0
106
107
108
109       val from_val : 'a -> 'a t
110
111
112       from_val v evaluates v first (as any function would) and returns an al‐
113       ready-forced suspension of its result.  It is the same as let x = v  in
114       lazy x , but uses dynamic tests to optimize suspension creation in some
115       cases.
116
117
118       Since 4.00.0
119
120
121
122       val map_val : ('a -> 'b) -> 'a t -> 'b t
123
124
125       map_val f x applies f directly if x is already forced, otherwise it be‐
126       haves as map f x .
127
128       When  x  is  already  forced, this behavior saves the construction of a
129       suspension, but on the other hand it performs more  work  eagerly  that
130       may not be useful if you never force the function result.
131
132       If f raises an exception, it will be raised immediately when is_val x ,
133       or raised only when forcing the thunk otherwise.
134
135       If map_val f x does not raise an exception, then is_val (map_val  f  x)
136       is equal to is_val x .
137
138
139       Since 4.13.0
140
141
142
143
144   Advanced
145       The  following definitions are for advanced uses only; they require fa‐
146       miliary with the lazy compilation scheme to be used appropriately.
147
148       val from_fun : (unit -> 'a) -> 'a t
149
150
151       from_fun f is the same as lazy (f ()) but slightly more efficient.
152
153       It should only be used if the function f is already defined.   In  par‐
154       ticular  it is always less efficient to write from_fun (fun () -> expr)
155       than lazy expr .
156
157
158       Since 4.00.0
159
160
161
162       val force_val : 'a t -> 'a
163
164
165       force_val x forces the suspension x and returns its result.  If  x  has
166       already  been  forced, force_val x returns the same value again without
167       recomputing it.
168
169       If the computation of x raises an exception, it is unspecified  whether
170       force_val x raises the same exception or Lazy.Undefined .
171
172
173       Raises  Undefined  if  the  forcing of x tries to force x itself recur‐
174       sively.
175
176
177
178
179   Deprecated
180       val lazy_from_fun : (unit -> 'a) -> 'a t
181
182       Deprecated.  synonym for from_fun .
183
184
185
186       val lazy_from_val : 'a -> 'a t
187
188       Deprecated.  synonym for from_val .
189
190
191
192       val lazy_is_val : 'a t -> bool
193
194       Deprecated.  synonym for is_val .
195
196
197
198
199
200OCamldoc                          2022-07-22                    Stdlib.Lazy(3)
Impressum