1Result(3) OCaml library Result(3)
2
3
4
6 Result - Result values.
7
9 Module Result
10
12 Module Result
13 : sig end
14
15
16 Result values.
17
18 Result values handle computation results and errors in an explicit and
19 declarative manner without resorting to exceptions.
20
21
22 Since 4.08
23
24
25
26
27
28
29
30 Results
31 type ('a, 'e) t = ('a, 'e) result =
32 | Ok of 'a
33 | Error of 'e
34
35
36 The type for result values. Either a value Ok v or an error Error e .
37
38
39
40 val ok : 'a -> ('a, 'e) result
41
42
43 ok v is Ok v .
44
45
46
47 val error : 'e -> ('a, 'e) result
48
49
50 error e is Error e .
51
52
53
54 val value : ('a, 'e) result -> default:'a -> 'a
55
56
57 value r ~default is v if r is Ok v and default otherwise.
58
59
60
61 val get_ok : ('a, 'e) result -> 'a
62
63
64 get_ok r is v if r is Ok v and
65
66
67 Raises Invalid_argument otherwise.
68
69
70
71 val get_error : ('a, 'e) result -> 'e
72
73
74 get_error r is e if r is Error e and
75
76
77 Raises Invalid_argument otherwise.
78
79
80
81 val bind : ('a, 'e) result -> ('a -> ('b, 'e) result) -> ('b, 'e)
82 result
83
84
85 bind r f is f v if r is Ok v and r if r is Error _ .
86
87
88
89 val join : (('a, 'e) result, 'e) result -> ('a, 'e) result
90
91
92 join rr is r if rr is Ok r and rr if rr is Error _ .
93
94
95
96 val map : ('a -> 'b) -> ('a, 'e) result -> ('b, 'e) result
97
98
99 map f r is Ok (f v) if r is Ok v and r if r is Error _ .
100
101
102
103 val map_error : ('e -> 'f) -> ('a, 'e) result -> ('a, 'f) result
104
105
106 map_error f r is Error (f e) if r is Error e and r if r is Ok _ .
107
108
109
110 val fold : ok:('a -> 'c) -> error:('e -> 'c) -> ('a, 'e) result -> 'c
111
112
113 fold ~ok ~error r is ok v if r is Ok v and error e if r is Error e .
114
115
116
117 val iter : ('a -> unit) -> ('a, 'e) result -> unit
118
119
120 iter f r is f v if r is Ok v and () otherwise.
121
122
123
124 val iter_error : ('e -> unit) -> ('a, 'e) result -> unit
125
126
127 iter_error f r is f e if r is Error e and () otherwise.
128
129
130
131
132 Predicates and comparisons
133 val is_ok : ('a, 'e) result -> bool
134
135
136 is_ok r is true iff r is Ok _ .
137
138
139
140 val is_error : ('a, 'e) result -> bool
141
142
143 is_error r is true iff r is Error _ .
144
145
146
147 val equal : ok:('a -> 'a -> bool) -> error:('e -> 'e -> bool) -> ('a,
148 'e) result -> ('a, 'e) result -> bool
149
150
151 equal ~ok ~error r0 r1 tests equality of r0 and r1 using ok and error
152 to respectively compare values wrapped by Ok _ and Error _ .
153
154
155
156 val compare : ok:('a -> 'a -> int) -> error:('e -> 'e -> int) -> ('a,
157 'e) result -> ('a, 'e) result -> int
158
159
160 compare ~ok ~error r0 r1 totally orders r0 and r1 using ok and error to
161 respectively compare values wrapped by Ok _ and Error _ . Ok _ values
162 are smaller than Error _ values.
163
164
165
166
167 Converting
168 val to_option : ('a, 'e) result -> 'a option
169
170
171 to_option r is r as an option, mapping Ok v to Some v and Error _ to
172 None .
173
174
175
176 val to_list : ('a, 'e) result -> 'a list
177
178
179 to_list r is [v] if r is Ok v and [] otherwise.
180
181
182
183 val to_seq : ('a, 'e) result -> 'a Seq.t
184
185
186 to_seq r is r as a sequence. Ok v is the singleton sequence containing
187 v and Error _ is the empty sequence.
188
189
190
191
192
193OCamldoc 2020-02-27 Result(3)