Skip to content
TurtleKitty edited this page May 11, 2019 · 2 revisions

ensure

This is the only global operator that affects both the user continuation and the error continuation. It is akin to unwind-protect or finally { } in other languages. It ensures that the given thunk executes whether the body expressions return normally or throw an error. Resuming an error in a guard handler defined outside the ensure could lead to inconsistency, so it's best to nest guards inside ensures, rather than the other way around.

No continuation can be captured across an ensure. It behaves like gate in this respect.

(def x (cell 5))

(proc handler ()
   (x.set! 5))

(ensure handler
   (x.set! 3)
   (+ 4 x.get))   ; 7

x                 ; #(cell 5)

(ensure handler
   (x.set! 3)
   (fail 23))     ; (runtime-error 23)

x                 ; #(cell 5)
Clone this wiki locally