"Adrian Cuthbertson" <adrian.cuthbert...@gmail.com> said: >> That said, I'd rather make sure that my low-level data structures are being >> operated on by only one implementation. > > You could use closures to encapsulate the refs/atoms ... > > (let [car-mem (ref nil)] > (defn set-car-mem [new-car-mem] > (dosync (ref-set car-mem new-car-mem))) > (defn update-car-mem [new-car-mem] > (dosync (set-car-mem new-car-mem))) > (defn get-car-mem [] @car-mem)) > > user=> (set-car-mem 0) > user=> (get-car-mem) > 0 > user=> @car-mem > java.lang.Exception: Unable to resolve symbol: car-mem in this context > (NO_SOURCE_FILE:0)
This is how I'd implemented state-hiding in Scheme [1]. The problem though, is exporting a public interface. In [1], (export x y) is just a macro for (define x null) and (define y null). So I define my public bindings and re-assign them to their actual values while I'm inside the closure. I can't do the same in Clojure, since I'm not allowed to re-assign a definition --i.e. its root. A dispatch function should do the trick [2]. [1] http://github.com/sindoc/algorithms/blob/master/src/main/scheme/memory-management/manual/pairs/pair.scm [2] http://github.com/sindoc/algorithms/blob/master/src/test/clojure/whiteboard/y2010/hide-adt-state-using-closure.clj > (note that you need a do-sync around ref-set - your code didn't have one.) 'set-car-mem' and the like are always called from within a dosync. I should probably remove those set-* functions since (1) they don't abstract too much and (2) they confuse readers. Thank you Adrian, for your help. Kind regards, SinDoc -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@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+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en