1Lazy(3) OCaml library Lazy(3)
2
3
4
6 Lazy - Deferred computations.
7
9 Module Lazy
10
12 Module Lazy
13 : sig end
14
15
16 Deferred computations.
17
18
19
20
21
22 type 'a t = 'a CamlinternalLazy.t
23
24
25 A value of type 'a Lazy.t is a deferred computation, called a suspen‐
26 sion, that has a result of type 'a . The special expression syntax
27 lazy (expr) makes a suspension of the computation of expr , without
28 computing expr itself yet. "Forcing" the suspension will then compute
29 expr and return its result. Matching a suspension with the special pat‐
30 tern syntax lazy(pattern) also computes the underlying expression and
31 tries to bind it to pattern :
32
33
34 let lazy_option_map f x = match x with | lazy (Some x) -> Some
35 (Lazy.force f x) | _ -> None
36
37 Note: If lazy patterns appear in multiple cases in a pattern-matching,
38 lazy expressions may be forced even outside of the case ultimately
39 selected by the pattern matching. In the example above, the suspension
40 x is always computed.
41
42 Note: lazy_t is the built-in type constructor used by the compiler for
43 the lazy keyword. You should not use it directly. Always use Lazy.t
44 instead.
45
46 Note: Lazy.force is not thread-safe. If you use this module in a
47 multi-threaded program, you will need to add some locks.
48
49 Note: if the program is compiled with the -rectypes option, ill-founded
50 recursive definitions of the form let rec x = lazy x or let rec x =
51 lazy(lazy(...(lazy x))) are accepted by the type-checker and lead, when
52 forced, to ill-formed values that trigger infinite loops in the garbage
53 collector and other parts of the run-time system. Without the -rec‐
54 types option, such ill-founded recursive definitions are rejected by
55 the type-checker.
56
57
58
59 exception Undefined
60
61
62
63
64
65 val force : 'a t -> 'a
66
67
68 force x forces the suspension x and returns its result. If x has
69 already been forced, Lazy.force x returns the same value again without
70 recomputing it. If it raised an exception, the same exception is
71 raised again. Raise Lazy.Undefined if the forcing of x tries to force
72 x itself recursively.
73
74
75
76 val force_val : 'a t -> 'a
77
78
79 force_val x forces the suspension x and returns its result. If x has
80 already been forced, force_val x returns the same value again without
81 recomputing it. Raise Lazy.Undefined if the forcing of x tries to
82 force x itself recursively. If the computation of x raises an excep‐
83 tion, it is unspecified whether force_val x raises the same exception
84 or Lazy.Undefined .
85
86
87
88 val from_fun : (unit -> 'a) -> 'a t
89
90
91 from_fun f is the same as lazy (f ()) but slightly more efficient.
92
93
94 from_fun should only be used if the function f is already defined. In
95 particular it is always less efficient to write from_fun (fun () ->
96 expr) than lazy expr .
97
98
99 Since 4.00.0
100
101
102
103 val from_val : 'a -> 'a t
104
105
106 from_val v returns an already-forced suspension of v . This is for
107 special purposes only and should not be confused with lazy (v) .
108
109
110 Since 4.00.0
111
112
113
114 val is_val : 'a t -> bool
115
116
117 is_val x returns true if x has already been forced and did not raise an
118 exception.
119
120
121 Since 4.00.0
122
123
124
125 val lazy_from_fun : (unit -> 'a) -> 'a t
126
127 Deprecated. synonym for from_fun .
128
129
130
131 val lazy_from_val : 'a -> 'a t
132
133 Deprecated. synonym for from_val .
134
135
136
137 val lazy_is_val : 'a t -> bool
138
139 Deprecated. synonym for is_val .
140
141
142
143
144
145OCamldoc 2019-07-30 Lazy(3)