Playing around a bit more:

(defprotocol Foo
  (bar [_] "do bar")
  (baz [_] "do baz"))

(defmacro defp [name arglist proto & bodies]
  (let [pairs (partition 2 bodies)
        protocol (resolve proto)
        namekw (keyword name)
        impls (reduce (fn [accum [t body]]
                        (let [tm (assoc
                                     (get accum t {})
                                   namekw `(fn ~arglist ~body))]
                          (assoc accum t tm)))
                      (get :impls protocol {})
                      pairs)]
    `(clojure.core/-reset-methods (alter-var-root ~protocol assoc
:impls ~impls))))

(defp bar [x] Foo
   String (concat x x)
   Character (list x x))

(defp baz [x] Foo
  String (rest (seq x))
  Character nil)

(and changing up the interface to it a bit) this seems to work.

Though, it doesn't return (satisfies? Foo String) correctly, but, I'll
either figure that out later, or ultimately not care.



On Mon, Feb 7, 2011 at 5:21 PM, Andrew Gwozdziewycz <apg...@gmail.com> wrote:
> On Mon, Feb 7, 2011 at 3:30 PM, Ken Wesson <kwess...@gmail.com> wrote:
>> On Mon, Feb 7, 2011 at 2:49 PM, Andrew Gwozdziewycz <apg...@gmail.com> wrote:
>>> On Mon, Feb 7, 2011 at 2:16 PM, Ken Wesson <kwess...@gmail.com> wrote:
>>>> Can't you just use extend-protocol to group your protocol
>>>> implementations for each record type in one place?
>>>
>>> That certainly works, but it's really not much different than:
>>>
>>> (defrecord Bar [x y]
>>>   Foo
>>>     (bar [_] ...)
>>>     (baz [_] ...)
>>>
>>> which is what I'm trying to avoid.
>>>
>>> It seems to me that the only reason that specifying the entire
>>> implementation together is to avoid situations where the
>>> implementation is not completely defined.
>>>
>>> See, I'm less concerned about implementing the Protocol than I am
>>> about creating "functions" that are dispatched by type, and since they
>>> have to have the same argument list, I see no reason why that
>>> boilerplate can't be eliminated.
>>>
>>> Perhaps one solution for me is to write a macro that constructs a
>>> protocol (and extends it to types) out of the definitions provided..
>>> sort of a hybrid of extend-protocol and defp from before.
>>>
>>> (defwhatever Foo
>>>   (bar [_]
>>>     String (body)
>>>     Character (body))
>>>   (baz [_]
>>>     String (body)
>>>     Character (body)))
>>>
>>> Which gets me to where I ultimately want to be (methods defined
>>> together per type extended too), but not quite what I'm really looking
>>> for.
>>
>> Now it sounds like you might want a plain old multimethod.
>
> It'd be more easily done with multimethods, sure, but since they all
> implement both methods, there's no reason to not use protocols. See
> the dilemma?
>
>
>> --
>> 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
>
>
>
> --
> http://www.apgwoz.com
>



-- 
http://www.apgwoz.com

-- 
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

Reply via email to