Import Upstream version 20180207
[hcoop/debian/mlton.git] / basis-library / util / dynamic-wind.sml
CommitLineData
7f918cf1
CE
1(* Copyright (C) 1999-2006, 2008 Henry Cejtin, Matthew Fluet, Suresh
2 * Jagannathan, and Stephen Weeks.
3 * Copyright (C) 1997-2000 NEC Research Institute.
4 *
5 * MLton is released under a BSD-style license.
6 * See the file MLton-LICENSE for details.
7 *)
8
9structure DynamicWind: DYNAMIC_WIND =
10struct
11
12fun try (f: unit -> 'a, k: 'a -> 'b, h: exn -> 'b) =
13 let
14 datatype t =
15 A of 'a
16 | E of exn
17 in
18 case A (f ()) handle e => E e of
19 A a => k a
20 | E e => h e
21 end
22
23fun wind (thunk, cleanup: unit -> unit) =
24 try (thunk, fn a => (cleanup (); a), fn e => (cleanup (); raise e))
25
26end