This commit is contained in:
hellerve
2016-09-12 17:56:36 +02:00
commit b809a26bb4
3 changed files with 96 additions and 0 deletions

21
context.zp Normal file
View File

@@ -0,0 +1,21 @@
(define-syntax with
(syntax-rules
"use a context manager. Takes a value, name and body
and binds value to name in body. Ensures resource cleanup.
params:
- val: the value to bind
- var: the name to bind to
- body: varargs for the body
complexity: O(1)
returns: the result of the resource cleanup"()
((_ val var body ...)
(let ((var val))
(begin
body ...
(teardown var))))))
(defprotocol context-manager ((teardown 1)))
(defimpl context-manager input-port? ((teardown close-input-file)))
(defimpl context-manager output-port? ((teardown close-output-file)))
(defimpl context-manager net:socket? ((teardown net:close-socket)))