Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lwt_result.catch takes a function as parameter now #965

Merged
merged 2 commits into from
Oct 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion CHANGES
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
===== dev =====

===== Build =====
====== Breaking API changes ======

* Lwt_result.catch now takes a function (unit -> 'a t) rather than a promise ('a t) (#965)

====== Build ======

* Remove unused dependency in dune file. (#969, Kate Deplaix)

Expand Down
2 changes: 1 addition & 1 deletion src/core/lwt_result.ml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ let map_err f e = map_error f e

let catch e =
Lwt.catch
(fun () -> ok e)
(fun () -> ok (e ()))
fail

let get_exn e =
Expand Down
6 changes: 3 additions & 3 deletions src/core/lwt_result.mli
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ 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] *)
val catch : (unit -> '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] *)

val get_exn : ('a, exn) t -> 'a Lwt.t
(** [get_exn] is the opposite of {!catch}: it unwraps the result type,
Expand Down
16 changes: 14 additions & 2 deletions test/core/test_lwt_result.ml
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,25 @@ let suite =

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

test "catch, error case"
(fun () ->
let x = Lwt.fail Dummy_error in
let x () = Lwt.fail Dummy_error in
Lwt.return (Lwt_result.catch x = Lwt_result.fail Dummy_error)
);

test "catch, bound raise"
(fun () ->
let x () = Lwt.bind Lwt.return_unit (fun () -> raise Dummy_error) in
Lwt.return (Lwt_result.catch x = Lwt_result.fail Dummy_error)
);

test "catch, immediate raise"
(fun () ->
let x () = raise Dummy_error in
Lwt.return (Lwt_result.catch x = Lwt_result.fail Dummy_error)
);

Expand Down