Details
-
Type:
Enhancement
-
Status:
Open
-
Priority:
Major
-
Resolution: Unresolved
-
Affects Version/s: None
-
Fix Version/s: Approved Backlog
-
Component/s: None
-
Labels:None
Description
Add the scope system for dealing with resource lifetime management
I think the decision of having to specify either a Closeable resource or a close function for an existing non-Closeable resource in with-open is quite awkward, because they have completely different meaning.
(let [foo (open-my-custom-resource "foo.bar")] (with-open [r (reader "foo.txt") foo #(.terminate foo)] (do-stuff r foo)))I think a CloseableResource protocol that can be extended to custom types as implemented in the patch to CLJ-308 is somewhat easier to use. Extend it once, and then you can use open-my-custom-resource in with-open just like reader/writer and friends...
That said, Scopes can still be useful, but I'd vote for handling the "how should that resource be closed" question by a protocol. Then the with-open helper can simply add
and cleanup-scope only needs to apply each fn without having to distinguish Closeables from fns.
(let [foo (open-my-custom-resource "foo.bar")] (with-open [r (reader "foo.txt") foo #(.terminate foo)] (do-stuff r foo)))