diff options
Diffstat (limited to 'share')
-rw-r--r-- | share/txr/stdlib/with-resources.tl | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/share/txr/stdlib/with-resources.tl b/share/txr/stdlib/with-resources.tl new file mode 100644 index 00000000..2a8c6791 --- /dev/null +++ b/share/txr/stdlib/with-resources.tl @@ -0,0 +1,17 @@ +(defmacro with-resources (res-bindings . body) + (tree-case res-bindings + (((var init cleanup) . rest) + ^(let ((,var ,init)) + (when ,var + (unwind-protect + (with-resources ,rest ,*body) + ,cleanup)))) + (((var init) . rest) + ^(let ((,var ,init)) + (with-resources ,rest ,*body))) + ((var . rest) + ^(let (,var) + (with-resources ,rest ,*body))) + (nil + ^(progn ,*body)) + (other (error "with-resources: bad syntax")))) |