Source file result.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
(** A replacement for {!Stdlib.Result} which
- is exception-safe,
- includes Lwt-, result-, and Lwt-result-aware traversors.
See {!Lwtreslib} and {!Seq} for general description of traversors and the
meaning of [_s], [_e], and [_es] suffixes. *)
module type S = sig
type ('a, 'e) t = ('a, 'e) result = Ok of 'a | Error of 'e
val ok : 'a -> ('a, 'e) result
val ok_s : 'a -> ('a, 'e) result Lwt.t
val error : 'e -> ('a, 'e) result
val error_s : 'e -> ('a, 'e) result Lwt.t
val value : ('a, 'e) result -> default:'a -> 'a
val value_f : ('a, 'e) result -> default:(unit -> 'a) -> 'a
val bind : ('a, 'e) result -> ('a -> ('b, 'e) result) -> ('b, 'e) result
val bind_error : ('a, 'e) result -> ('e -> ('a, 'f) result) -> ('a, 'f) result
val map : ('a -> 'b) -> ('a, 'e) result -> ('b, 'e) result
val map_error : ('e -> 'f) -> ('a, 'e) result -> ('a, 'f) result
val bind_s :
('a, 'e) result -> ('a -> ('b, 'e) result Lwt.t) -> ('b, 'e) result Lwt.t
val bind_error_s :
('a, 'e) result -> ('e -> ('a, 'f) result Lwt.t) -> ('a, 'f) result Lwt.t
val join : (('a, 'e) result, 'e) result -> ('a, 'e) result
val map_e : ('a -> ('b, 'e) result) -> ('a, 'e) result -> ('b, 'e) result
val map_s : ('a -> 'b Lwt.t) -> ('a, 'e) result -> ('b, 'e) result Lwt.t
val map_es :
('a -> ('b, 'e) result Lwt.t) -> ('a, 'e) result -> ('b, 'e) result Lwt.t
val map_error_e :
('e -> ('a, 'f) result) -> ('a, 'e) result -> ('a, 'f) result
val map_error_s : ('e -> 'f Lwt.t) -> ('a, 'e) result -> ('a, 'f) result Lwt.t
val map_error_es :
('e -> ('a, 'f) result Lwt.t) -> ('a, 'e) result -> ('a, 'f) result Lwt.t
val fold : ok:('a -> 'c) -> error:('e -> 'c) -> ('a, 'e) result -> 'c
val iter : ('a -> unit) -> ('a, 'e) result -> unit
val iter_s : ('a -> unit Lwt.t) -> ('a, 'e) result -> unit Lwt.t
val iter_error : ('e -> unit) -> ('a, 'e) result -> unit
val iter_error_s : ('e -> unit Lwt.t) -> ('a, 'e) result -> unit Lwt.t
val is_ok : ('a, 'e) result -> bool
val is_error : ('a, 'e) result -> bool
val equal :
ok:('a -> 'a -> bool) ->
error:('e -> 'e -> bool) ->
('a, 'e) result ->
('a, 'e) result ->
bool
val compare :
ok:('a -> 'a -> int) ->
error:('e -> 'e -> int) ->
('a, 'e) result ->
('a, 'e) result ->
int
val to_option : ('a, 'e) result -> 'a option
val of_option : error:'e -> 'a option -> ('a, 'e) result
val to_list : ('a, 'e) result -> 'a list
val to_seq : ('a, 'e) result -> 'a Stdlib.Seq.t
(** [catch f] is [try Ok (f ()) with e -> Error e]: it is [Ok x] if [f ()]
evaluates to [x], and it is [Error e] if [f ()] raises [e].
See {!WithExceptions.S.Result.to_exn} for a converse function.
If [catch_only] is set, then only exceptions [e] such that [catch_only e]
is [true] are caught.
Whether [catch_only] is set or not, this function never catches
non-deterministic runtime exceptions of OCaml such as {!Stack_overflow}
and {!Out_of_memory}. *)
val catch : ?catch_only:(exn -> bool) -> (unit -> 'a) -> ('a, exn) result
(** [catch_f f handler] is equivalent to [map_error (catch f) handler].
In other words, it catches exceptions in [f ()] and either returns the
value in an [Ok] or passes the exception to [handler] for the [Error].
No attempt is made to catch the exceptions raised by [handler].
[catch_only] has the same use as with [catch]. The same restriction on
catching non-deterministic runtime exceptions applies. *)
val catch_f :
?catch_only:(exn -> bool) -> (unit -> 'a) -> (exn -> 'e) -> ('a, 'e) result
(** [catch_ef f handler] is equivalent to [join @@ map_error (catch f) handler].
In other words, it catches exceptions in [f ()] and either returns the
value as is or passes the exception to [handler] for the [Error]. The
handler must return an error of the same type as that carried by [f ()].
No attempt is made to catch the exceptions raised by [handler].
[catch_only] has the same use as with [catch]. The same restriction on
catching non-deterministic runtime exceptions applies. *)
val catch_ef :
?catch_only:(exn -> bool) ->
(unit -> ('a, 'error) result) ->
(exn -> 'error) ->
('a, 'error) result
(** [catch_s] is [catch] but for Lwt promises. Specifically, [catch_s f]
returns a promise that resolves to [Ok x] if and when [f ()] resolves to
[x], or to [Error exc] if and when [f ()] is rejected with [exc].
If [catch_only] is set, then only exceptions [e] such that [catch_only e]
is [true] are caught.
Whether [catch_only] is set or not, this function never catches
non-deterministic runtime exceptions of OCaml such as {!Stack_overflow}
and {!Out_of_memory}. *)
val catch_s :
?catch_only:(exn -> bool) -> (unit -> 'a Lwt.t) -> ('a, exn) result Lwt.t
(** We do not provide [catch_s_f] because (a) the suffix becomes confusing,
(b) it's not used, (c) it is not obvious whether we want the handler to be
within Lwt (gives more flexibility) or not (gives more guarantee about the
timeliness of learning about rejections). We will revisit this if a needs
for it arises. *)
(**/**)
val return : 'a -> ('a, 'e) result
val return_unit : (unit, 'e) result
val return_none : ('a option, 'e) result
val return_some : 'a -> ('a option, 'e) result
val return_nil : ('a list, 'e) result
val return_true : (bool, 'e) result
val return_false : (bool, 'e) result
end