--
--
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.
The part I am struggling with is how to create a Woobly without exposing its internals.
--
--
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 a topic in the Google Groups "Clojure" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/clojure/D2OBBPTxGfY/unsubscribe?hl=en.
To unsubscribe from this group and all its topics, send an email to clojure+u...@googlegroups.com.
I am nervous as well about "expose internals but trust people to do the right thing" because in this case, if I was a consumer and saw that queue, particularly given the emphasis about data being the contract etc. then why would I think *not* to use it.
--
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.
I agree with the advice you've gotten, but since no one has mentioned it, I wanted to point out that you can have encapsulation w/o protocols with something like this.
Assume a queue is your only state and `add` and `clear` are your private fns that take a queue as first argument.
(defn new-scheduler []
(let [queue (...)]
{:add (partial add queue)
:clear (partial clear queue)}))
There are several disadvantages to this, however. The biggest in my book is that it achieves your goal, and you're limited in the same way your users are. You can't add behavior to an already created scheduler (unless it's built on adding and clearing). Furthermore, if you dynamically recompile `add` or `clear`, it won't change the behavior of an already created scheduler, since partial has the fns, not the symbols or vars that point at them. (These same disadvantages apply to a reified protocol.)
As others have recommended, just return a map. Keep in mind that the documentation is just a `(doc new-scheduler)` away and that auto-completion will tend to send people back into your ns's fns rather than into the internals of a data structure.
The add method that you partially apply in new-scheduler should be private, because a user can't supply the first argument it expects. You might do something like this.
(defn- add* [queue item] (...))
(defn add [scheduler item]
((scheduler :add) item))
(defn new-scheduler []
(let [queue (...)]
{:add (partial add* queue)}))
But again, I am not recommending this. I just wanted to point out that a closure gives you encapsulation w/o extra language features.
And please don't think I am making the 'code should stop bad programmers doing the wrong thing' argument, I'm not (been there, lost). I just know that if I released a scheduler library and the main construct was a map containing a LBQ (or in fact any sequence) then I would be inundated with a bunch of 'I did X to the queue and it broke'....
Have you tried it? :)I've authored about 40 Clojure libraries over 5 years, some with data structures with internal components. The number of times someone has said "I did X to this internal data and it broke" is exactly zero. It's never ever happened.So I think you're probably borrowing trouble, and I'd certainly be very surprised if you have any support requests like the one you describe.
This is all about changing my mindset from 15-odd years of Java dev by learning from others, so let's give it a go. Years of enterprise Java dev have gotten their cynical, 'data is precious and should be hidden away', 'other devs will do the wrong thing' etc. claws into me though, so it is with trepidation I set out on this gloriously liberating new path :).
--