module BatResult:sig..end
Monadic results of computations that can raise exceptions
type('a, 'e)t =('a, 'e) BatPervasives.result=
| |
Ok of |
| |
Error of |
The type of a result. A result is either Ok x carrying the
normal return value x or is Error e carrying some indication of an
error. The value associated with a bad result is usually an exception
(exn) that can be raised.
val ok : 'a -> ('a, 'b) tok v is Ok v.
val error : 'e -> ('a, 'e) terror e is Error e.
val value : ('a, 'e) t -> default:'a -> 'avalue r ~default is v if r is Ok v and default otherwise.
val default : 'a -> ('a, 'b) t -> 'adefault d r evaluates to d if r is Error else x when r is
Ok x.
value or a slightly different signature.val get_ok : ('a, 'e) t -> 'aget_ok r is v if r is Ok v and
Invalid_argument otherwise.val get_error : ('a, 'e) t -> 'eget_error r is e if r is Error e and
Invalid_argument otherwise.val get : ('a, exn) t -> 'aget (Ok x) returns x, and get (Error e) raises e. This
function is, in a way, the opposite of the catch function
val catch : ('a -> 'e) -> 'a -> ('e, exn) tExecute a function and catch any exception as a result. This function encapsulates code that could throw an exception and returns that exception as a value.
val catch2 : ('a -> 'b -> 'c) -> 'a -> 'b -> ('c, exn) tAs catch but two parameters. This saves a closure construction
val catch3 : ('a -> 'b -> 'c -> 'd) -> 'a -> 'b -> 'c -> ('d, exn) tAs catch but three parameters. This saves a closure construction
val bind : ('a, 'e) t -> ('a -> ('b, 'e) t) -> ('b, 'e) tbind r f is f v if r is Ok v and r if r is Error _.
val join : (('a, 'e) t, 'e) t -> ('a, 'e) tjoin rr is r if rr is Ok r and rr if rr is Error _.
val map : ('a -> 'b) -> ('a, 'e) t -> ('b, 'e) tmap f r is Ok (f v) if r is Ok v and r if r is Error _.
val map_error : ('e -> 'f) -> ('a, 'e) t -> ('a, 'f) tmap_error f r is Error (f e) if r is Error e and r if
r is Ok _.
val map_both : ('a1 -> 'a2) ->
('b1 -> 'b2) -> ('a1, 'b1) t -> ('a2, 'b2) tmap_both f g (Ok x) returns Ok (f x) and map_both f g (Error e) returns Error (g e).
val map_default : 'b -> ('a -> 'b) -> ('a, 'c) t -> 'bmap_default d f r evaluates to d if r is Error else f x
when r is Ok x
val fold : ok:('a -> 'c) -> error:('e -> 'c) -> ('a, 'e) t -> 'cfold ~ok ~error r is ok v if r is Ok v and error e if r
is Error e.
val iter : ('a -> unit) -> ('a, 'e) t -> unititer f r is f v if r is Ok v and () otherwise.
val iter_error : ('e -> unit) -> ('a, 'e) t -> unititer_error f r is f e if r is Error e and () otherwise.
val is_ok : ('a, 'e) t -> boolis_ok (Ok _) is true, otherwise false.
val is_error : ('a, 'e) t -> boolis_error r is true iff r is Error _.
val is_bad : ('a, 'e) t -> boolSame as is_error.
val is_exn : exn -> ('a, exn) t -> boolis_exn e1 r is true iff r is Error e2 with e1=e2
val equal : ok:('a -> 'a -> bool) ->
error:('e -> 'e -> bool) ->
('a, 'e) t -> ('a, 'e) t -> boolequal ~ok ~error r0 r1 tests equality of r0 and r1 using ok
and error to respectively compare values wrapped by Ok _ and
Error _.
val compare : ok:('a -> 'a -> int) ->
error:('e -> 'e -> int) ->
('a, 'e) t -> ('a, 'e) t -> intcompare ~ok ~error r0 r1 totally orders r0 and r1 using ok and
error to respectively compare values wrapped by Ok _ and Error _.
Ok _ values are smaller than Error _ values.
val to_option : ('a, 'b) t -> 'a optionto_option r is r as an option, mapping Ok v to Some v and
Error _ to None.
val of_option : 'a option -> ('a, unit) tConvert an option to a result
val to_list : ('a, 'e) t -> 'a listto_list r is [v] if r is Ok v and [] otherwise.
val to_seq : ('a, 'e) t -> 'a BatSeq.tto_seq r is r as a sequence. Ok v is the singleton sequence
containing v and Error _ is the empty sequence.
This monad is very similar to the option monad, but instead of
being None when an error occurs, the first error in the sequence is
preserved as the return value.
module Monad:sig..end
module Infix:sig..end
This infix module provides the operator (>>=)
val print : ('b BatInnerIO.output -> 'a -> unit) ->
'b BatInnerIO.output -> ('a, exn) t -> unitPrint a result as Ok(x) or Error(exn)