I have some code which opens a bunch of resources (say, files), and
needs to make sure they all get closed. Something like this:
(let [files (atom [])]
(try
(open-my-many-files files)
;; the files atom now refers to a vector of open file handles
(do-dangerous-io @files)
(finally
you are using an atom as a temporary variable. please don't do that.
(doseq [f files]
(with-open [of (open-file f)]
(do-dangerous-io of)))
On Sun, Sep 13, 2009 at 1:05 PM, Constantine Vetoshev
wrote:
>
> I have some code which opens a bunch of resources (say, files), and
> needs to make
Apparently I oversimplified my example. In reality, I am trying to
deal with a bunch of open Berkeley DB cursors which I need to combine.
They cannot be processed serially. I need to ensure that all those
cursors close at the end even if an exception occurs, so I need to
loop over the resulting li
> Clojure does not seem to support iteration inside catch or finally
> forms.
Apparently not directly. Splitting the iteration into a separate named
function works, though:
(defn print-seq [foo]
(doseq [x foo]
(println x)))
(let [foo (atom [1 2 3])]
(try
(throw (new Exceptio
user=> (macroexpand '(with-open [x A y Y] do stuff here))
(let* [x A] (try (clojure.core/with-open [y Y] do stuff here) (finally
(. x clojure.core/close
user=>
with-open expands to a (try (finally (.close ...)))
On Sun, Sep 13, 2009 at 7:38 PM, Richard Newman wrote:
>
>> Clojure does not se
I think the OP's issue was that he has a sequence of things to close,
determined at runtime. with-open itself is no use there, because the
things to close must be known at compile-time.
He is correct that finally and catch do not permit iteration. They do
permit function calls, hence my wor
On Sep 13, 11:18 pm, Richard Newman wrote:
> I think the OP's issue was that he has a sequence of things to close,
> determined at runtime. with-open itself is no use there, because the
> things to close must be known at compile-time.
Exactly.
> He is correct that finally and catch do not p