File: stdcompat__fun.ml.in

package info (click to toggle)
ocaml-stdcompat 19-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 6,516 kB
  • sloc: ml: 27,806; sh: 875; makefile: 246
file content (33 lines) | stat: -rw-r--r-- 844 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
@BEGIN_FROM_4_08_0@
include Fun
@END_FROM_4_08_0@
@BEGIN_BEFORE_4_08_0@
external id : 'a -> 'a = "%identity"
(** @since 4.08.0: external id : 'a -> 'a = "%identity" *)

let const c _ = c

let flip f x y = f y x

let negate f x = not (f x)

exception Finally_raised of exn

let protect ~finally f =
  let value =
    try
      Stdcompat__pervasives.Ok (f ())
    with exn ->
      let bt = Stdcompat__printexc.get_raw_backtrace () in
      Stdcompat__pervasives.Error (exn, bt) in
  begin
    try
      finally ()
    with exn ->
      let bt = Stdcompat__printexc.get_raw_backtrace () in
      Stdcompat__printexc.raise_with_backtrace (Finally_raised exn) bt
  end;
  match value with
  | Stdcompat__pervasives.Ok result -> result
  | Stdcompat__pervasives.Error (exn, bt) -> Stdcompat__printexc.raise_with_backtrace exn bt
@END_BEFORE_4_08_0@