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