Two options. The first, straightforward one: make the thing that's 
implementing the protocol the Storage, not in the individual things being 
stored. So something like:

(defprotocol ItemStore
  (save-item [store item])
  (load-items [store items]))

etc.

Second option: Clojure has multimethods too, though (as I understand them), 
they aren't as powerful or free-form. Combined with marker protocols 
though, you can probably put together something that works.

;; Marker protocol, doesn't have any methods attached to it
(defprotocol Loadable)

(defrecord LoadableThing [my-data]
  Loadable) ;; Let Clojure knows that it satisfies the protocol

;; Define a dispatch function that can figure out which multimethod to call.
(defn loadable-dispatch
  [item]
  (cond (satisfies? LoadableThing item) :load-single

On Sunday, August 28, 2016 at 7:29:46 AM UTC-7, Jonathon McKitrick wrote:
>
> I'm beginning a foray into protocols after coming from the Common Lisp 
> world of multimethods. I'm using them initially to implement a consistent 
> load/save API over the DB layer.
>
> It's pretty simple to have a Saveable protocol to save one object, because 
> the first argument is a Record of the type I am saving.
>
> But since protocols dispatch on the first argument, how do you define a 
> Loadable protocol that loads an object or handles a collection of objects?
>
> For example, if I want to load an object by idx, the first argument is not 
> a Record, but an integer, or perhaps an arbitrary field to used in a query.
>
>
>

-- 
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
--- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to