Skip to content

Commit

Permalink
Add Lwt_result.error
Browse files Browse the repository at this point in the history
For consistency with Stdlib.
  • Loading branch information
MisterDA committed Feb 1, 2022
1 parent 703b086 commit 982ea05
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
====== Additions ======

* In the Lwt_io module, add `?cloexec:bool` optional arguments to functions that create file descriptors (`pipe`). The `?cloexec` argument is simply forwarded to the wrapped Lwt_unix function. (#872, #911, Antonin Décimo)
* Add Lwt_result.iter and Lwt_result.iter_error for consistency with Stdlib. (#927, Antonin Décimo)
* Add Lwt_result.error, Lwt_result.iter, and Lwt_result.iter_error for consistency with Stdlib. (#927, Antonin Décimo)

====== Fixes ======

Expand Down
1 change: 1 addition & 0 deletions src/core/lwt_result.ml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ let fail e = Lwt.return (Error e)

let lift = Lwt.return
let ok x = Lwt.map (fun y -> Ok y) x
let error x = Lwt.map (fun y -> Error y) x

let map f e =
Lwt.map
Expand Down
3 changes: 3 additions & 0 deletions src/core/lwt_result.mli
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ val lift : ('a, 'b) Result.result -> ('a, 'b) t

val ok : 'a Lwt.t -> ('a, _) t

val error : 'b Lwt.t -> (_, 'b) t
(** @since 5.6.0 *)

val catch : 'a Lwt.t -> ('a, exn) t
(** [catch x] behaves like [return y] if [x] evaluates to [y],
and like [fail e] if [x] raises [e] *)
Expand Down
6 changes: 6 additions & 0 deletions test/core/test_lwt_result.ml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ let suite =
Lwt.return (Lwt_result.ok x = Lwt_result.return 0)
);

test "error"
(fun () ->
let x = Lwt.return 0 in
Lwt.return (Lwt_result.error x = Lwt_result.fail 0)
);

test "catch"
(fun () ->
let x = Lwt.return 0 in
Expand Down

0 comments on commit 982ea05

Please sign in to comment.