[ANN] scopes: a little library for resource scopes

194 views
Skip to first unread message

Paul Stadig

unread,
Oct 11, 2013, 6:36:45 AM10/11/13
to clo...@googlegroups.com
I have released version 0.1.0 of scopes, a little library for resource scopes. It can be used to establish a resource scope with the with-resource-scope macro. Within the dynamic extent of that macro one can register a resource with the scoped! function so that when the macrco block goes out of scope .close is called on the registered resource.

An object and function can also be registered with (scoped! x (fn [x] ...)) so that when the macro block goes out of scope the function will be called and given x as an argument.

There is also a shorthand function called scoped-thunk! that will register a thunk (a function taking no arguments) to be called when the macro block goes out of scope.

The scopes library also defines a function named closeable? that will return true if its argument implements java.lang.AutoCloseable.

Because scopes uses the java.lang.AutoCloseable interface and the exception supression mechanism, it is only compatible with Java 7.

Finally, there is also a scopes-magic artifact, and when it is on the classpath it will automatically pull in the scopes library and add with-resource-scope, scoped!, scoped-thunk!, and closeable? to clojure.core so that they are automatically available everywhere with out having to be imported. I'm not saying that this is necessarily a good idea, but there it is. :)

https://clojars.org/pjstadig/scopes
https://clojars.org/pjstadig/scopes-magic


Paul

Cedric Greevey

unread,
Oct 11, 2013, 7:56:52 AM10/11/13
to clo...@googlegroups.com
What does this do that (with-open ...) doesn't do? Generalize to other cleanup methods than (.close x)?


--
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
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 clojure+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Paul Stadig

unread,
Oct 11, 2013, 9:54:59 AM10/11/13
to clo...@googlegroups.com
It separates the resource management from the creation and lexical binding of resources. You can create a scope high up in the call stack, and within the dynamic extent of that scope you can create resources, return them from functions and freely use them; when the scope eventually exits, then the resources will be cleaned up.

This library does also generalize a bit to allow you to register an object and a clean up function, or even just a thunk to get called when the scope exits.

Jozef Wagner

unread,
Oct 12, 2013, 10:11:57 AM10/12/13
to clo...@googlegroups.com

Paul Stadig

unread,
Oct 13, 2013, 7:34:01 AM10/13/13
to clo...@googlegroups.com
No, I just implemented a simple intuitive scope, which looks like it is about the same as the basic idea there. There are some interesting ideas/questions about how scopes work across threads and how consumers can pass the buck for resource clean up to an enclosing scope (like exception handling).

In my case I explicitly don't handle extending scopes to child threads (for now). If the bindings convey to a child thread and you try to add an object to the current scope, then you'll get an exception for trying to set! a conveyed binding.

I'm not sure about the idea for passing resource clean up to an enclosing scope. I'd have to consider the problem and potential solutions before I could form an opinion.

That's not to say that these things are bad ideas or that some version of them won't end up in the library. I just wanted a minimal implementation of a scoping concept. It can evolve from there.


Paul


You received this message because you are subscribed to a topic in the Google Groups "Clojure" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/clojure/qPUd3AEVxT8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to clojure+u...@googlegroups.com.

Jozef Wagner

unread,
Oct 13, 2013, 8:31:59 AM10/13/13
to clo...@googlegroups.com
In JVM, we can assume (or should implement it in a way) that objects representing resource release the resource upon object's finalize method. If you push resource into the scope, it is not GCed until the scope ends. How about pushing to scope weak refs to resources instead?

JW
Reply all
Reply to author
Forward
0 new messages