dynamic_wind(in, body, out)
Return the result of evaluating body
.
Dynamic_wind
is useful for performing tasks that must be performed whenever control enters or
leaves body
, either normally or by continuation application.
The three arguments - in
, body
, and out
- must
be functions that accept zero arguments. Before applying body
, and each time body
is entered subsequently by the application of a continuation created within body
,
the in
function is called. Upon normal exit from body
and each time body
is
exited by the application of a continuation created outside body
, the out
function is applied.
Thus, it is guaranteed that in
is invoked at least once. In addition, if body
ever returns,
out
is invoked at least once.
dynamic_wind(^() showln("hello!"),
^() showln("doing some work..."),
^() showln("goodbye!"))
//> hello!
doing some work...
goodbye!
let kk = false
dynamic_wind(^() showln("hello!"),
^() { callcc(^(k) kk = k)
showln("doing some work...") },
^() showln("goodbye!"))
//> hello!
doing some work...
goodbye!
kk()
//> hello!
doing some work...
goodbye!
dynamic_wind(^() showln("hello!"),
^() { raise('blah)
showln("doing some work...") },
^() showln("goodbye!"))
//> hello!
goodbye!
//> error: This object was raised: blah