I'm posting this on the off chance it's useful to those of you writing REPL
shell tools or I/O abstractions (e.g. java's NIO.2 DirectoryStream
wrappers) that might benefit from a close-when-GC'd backstop.
For example, clj-nio2 has a nice little lazy directory sequence function:
(defn- lazy-dir-stream-seq [^DirectoryStream dir-stream]
(let [it (.iterator dir-stream)
iter (fn thisfn []
(if (.hasNext it)
(cons (.next it)
(lazy-seq (thisfn)))
(.close dir-stream)))]
(lazy-seq (iter))))
However that will result in the stream being closed only if we reach the
end of the directory stream.
So I wrote the code in the referenced gist to address that problem, though
I haven't hooked it up into my own or one of the existing NIO wrappers yet.
So FWIW. And suggestions welcome.
I hate the bit of reflection I used but was having difficulty doing it any
other way with PhantomReference instances.
https://gist.github.com/dtenny/9104215
Basically any open Closeable reference can be called in an (ensure-close
<x>) fashion to make sure it gets closed eventually,
as my exhaustive (NOT!) test case at the end shows (file content gets
flushed).
It's very side effect-y, I feel so dirty... Bad java followed by worse
clojure. Tips welcome. It was just a clojure learning thing for me.
--
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
---
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.