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