Hi,
(defmacro with-resources
[bindings close-fn & body]
(let [[x v & more] bindings]
`(let [~x ~v]
(try
~(if-let [more (seq more)]
`(with-resources ~more ~close-fn ~@body)
`(do ~@body))
(finally
(~close-fn ~x))))))
user=> (macroexpand-1 '(with-resources [x 1] close foo bar))
(clojure.core/let [x 1] (try (do foo bar) (finally (close x))))
user=> (macroexpand-1 '(with-resources [x 1 y 2] close foo bar))
(clojure.core/let [x 1] (try (user/with-resources (y 2) close foo bar)
(finally (close x))))
You can call with-resource recursively. This has the added benefit of
closing the resources in reverse order.
Sincerely
Meikel
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en